Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the color of auto detected links on UITextView?

People also ask

Can you change the color of hyperlinks?

Change the color of a hyperlink Select the hyperlink you want to re-color. (How do I insert a hyperlink?) On the Home tab of the ribbon, select the Font Color arrow to open the menu of colors. Select the color you want for the hyperlink.

How do I change the color of my links on my Iphone?

You can right-click on the URL, and from the secondary menu, choose Font ▸ Show Fonts. You can then click the Text color button, and change the color of the text and link, but there is no control at this level over different text and link underline colors.

Did Google change the color of links?

Google is testing out a new color for its search result links, and it's not a shade of the tried-and-true blue. It's black. The tech giant is currently experimenting with black links in search results, so if you're still seeing classic blue links instead, consider yourself lucky.


On iOS 7 you can set the tintColor of the UITextView. It affects the link color as well as the cursor line and the selected text color.

iOS 7 also added a new property to UITextView called linkTextAttributes which would appear to let you fully control the link style.


You can use UIAppearance protocol to apply changes for all text views:

Swift 4.x:

UITextView.appearance().linkTextAttributes = [ .foregroundColor: UIColor.red ]

Swift 3.x:

UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.red ]

Swift 2.x:

UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.redColor() ]

Objective-C:

[UITextView appearance].linkTextAttributes = @{ NSForegroundColorAttributeName : UIColor.redColor };

Appearance for UITextView is not documented, but works well.

Keep in mind UIAppearance notes:

iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.

In other words: Calling this code in init(), or init(coder:) methods will change UI Object appearance, but calling in loadView(), or viewDidLoad() of viewController won't.
If you want to set appearance for whole application, application(_:didFinishLaunchingWithOptions:) is good place for calling such code.


Instead of using an UITextView, I used an UIWebView and enabled the "auto-detect links". To change the link color, just created a regular CSS for the tag.

I used something like this:

NSString * htmlString = [NSString stringWithFormat:@"<html><head><script> document.ontouchmove = function(event) { if (document.body.scrollHeight == document.body.clientHeight) event.preventDefault(); } </script><style type='text/css'>* { margin:0; padding:0; } p { color:black; font-family:Helvetica; font-size:14px; } a { color:#63B604; text-decoration:none; }</style></head><body><p>%@</p></body></html>", [update objectForKey:@"text"]];
webText.delegate = self;
[webText loadHTMLString:htmlString baseURL:nil];

The problem with UITextView linkTextAttributes is that it applies to all automatically detected links. What if you want different links to have different attributes?

It turns out there's a trick: configure the links as part of the text view's attributed text, and set the linkTextAttributes to an empty dictionary.

Here's an example in iOS 11 / Swift 4:

// mas is the mutable attributed string we are forming...
// ... and we're going to use as the text view's `attributedText`
mas.append(NSAttributedString(string: "LINK", attributes: [
    NSAttributedStringKey.link : URL(string: "https://www.apple.com")!,
    NSAttributedStringKey.foregroundColor : UIColor.green,
    NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]))
// ...
self.tv.attributedText = mas
// this is the important thing:
self.tv.linkTextAttributes = [:]

You can Change the Hyperlink Color in a TextView by the following:

In the Nib file, you can go to the Properties Window and change the Tint to which ever color you want to.

or you can also do it programatically by using the below code

[YOURTEXTVIEW setTintColor:[UIColor whiteColor]];