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

IphoneUitextviewIphone Sdk-3.0Uicolor

Iphone Problem Overview

I had a UITextView that detects phone numbers and links, but this overrides my fontColor and change it to blueColor. Is there a way to format the color of auto detected links, or should I try a manual version of this function?

Iphone Solutions

Solution 1 - Iphone

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.

Solution 2 - Iphone

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

Swift 4.x+:

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

Swift 3.x:

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

Swift 2.x:

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


[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.

Solution 3 - Iphone

Solution 4 - Iphone

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: [ : URL(string: "")!,
    NSAttributedStringKey.foregroundColor :,
    NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
// ... = mas
// this is the important thing: = [:]

Solution 5 - Iphone

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]];

Solution 6 - Iphone

Swift 5 Answer

Nice and simple

myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]

Solution 7 - Iphone

The tint color can be done in the storyboard also

enter image description here

Solution 8 - Iphone

I found indeed another way without using a webview but keep in mind that this uses private API and may be rejected in appstore:

EDIT: My app got approved by apple although the private api usage!

First declare a category on UITextView with the methods

- (id)contentAsHTMLString;
- (void)setContentToHTMLString:(id)arg1;

They are just doing the following:

- (id)contentAsHTMLString;
    return [super contentAsHTMLString];

- (void)setContentToHTMLString:(id)arg1;
    [super setContentToHTMLString:arg1];

Now write a method for colorful links:

- (void) colorfillLinks;
    NSString *contentString = [self.textViewCustomText contentAsHTMLString];
    contentString = [contentString stringByReplacingOccurrencesOfString:@"x-apple-data-detectors=\"true\""
                                         withString:@"x-apple-data-detectors=\"true\" style=\"color:white;\""];
    [self.textViewCustomText setContentToHTMLString:contentString];

It does set the style attribute with a specific color on all types of links.

UITextViews are rendered Webiview like via divs so you could even go further and color each link type separately:

<div><a href="" x-apple-data-detectors="true" style="color:white;" x-apple-data-detectors-type="link" x-apple-data-detectors-result="0"></a></div>

The x-apple-data-detectors-type="link" is the indicator for the exact type of the link


On iOS7this is no longer working. In iOS7 you could easily change the link color of UITextViews by setting the tint color. You should not call

- (id)contentAsHTMLString;

anymore, you'll get an exception. Instead do the following if you want to support iOS 7 and below:

- (void) colorfillLinks;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.tintColor = [UIColor colorWithRed:79.0/255.0
} else if(![self isFirstResponder ]) {
    NSString *contentString = [self contentAsHTMLString];
    contentString = [contentString stringByReplacingOccurrencesOfString:@"x-apple-data-detectors=\"true\""
                                                             withString:@"x-apple-data-detectors=\"true\" style=\"color:#DDDDDE;\""];
    [self setContentToHTMLString:contentString];

Solution 9 - Iphone

EDIT: Don't do it with UITextView, use UIWebView instead.

You need to make a stylesheet for that. Define a class there with the color combination you need-

.headercopy {
 font-family: "Helvetica";
 font-size: 14px;
 line-height: 18px;
 color: #25526e;
a.headercopy:link {
a.headercopy:hover {
a.headercopy:visited {
a.headercopy:hover {

now use the class 'headercopy' into you html page like this-

<b>Fax:</b><a href="tel:646.200.7535" class="headercopy"> 646-200-7535</a><br />

this will display the phone number in the color you need with click functionality.

Solution 10 - Iphone

This code will set the colour of a phone number on an I-Phone but voids the automatic call link.

<div><a href="#" x-apple-data-detectors="true" style="color:white;" x-apple-data-detectors-type="link" x-apple-data-detectors-result="0">p&nbsp;&nbsp;0232 963 959</a></div>

Solution 11 - Iphone

This is how I did it using Swift 5:

let attributedString = NSMutableAttributedString(string: myTextView.text ?? "")
myTextView.linkTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.whiteColor] as [NSAttributedString.Key: Any]?
myTextView.attributedText = attributedString


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeandro AlvesView Question on Stackoverflow
Solution 1 - IphonestonemonkView Answer on Stackoverflow
Solution 2 - IphoneGrubasView Answer on Stackoverflow
Solution 3 - IphoneLeandro AlvesView Answer on Stackoverflow
Solution 4 - IphonemattView Answer on Stackoverflow
Solution 5 - IphoneManjuView Answer on Stackoverflow
Solution 6 - IphoneMark WilsonView Answer on Stackoverflow
Solution 7 - IphoneRyan HeitnerView Answer on Stackoverflow
Solution 8 - IphoneAlexanderView Answer on Stackoverflow
Solution 9 - IphoneVaibhav SaranView Answer on Stackoverflow
Solution 10 - IphoneMark OvertonView Answer on Stackoverflow
Solution 11 - IphonejaytrixzView Answer on Stackoverflow