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

11

132

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?

Carnes answered 29/8, 2009 at 4:9 Comment(1)
Check this answer using a private-api subview UIWebDocumentView: https://mcmap.net/q/174919/-change-color-of-uitextview-links-with-a-filterSiam
R
195

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.

Rarity answered 27/9, 2013 at 10:30 Comment(2)
I've given further explanation in this answer: https://mcmap.net/q/174920/-change-uitextview-hyperlink-color-duplicateDarice
On iOS 10.3, I wasn't able to override the NSFontAttributeName with linkTextAttributes. I had to manually specify the font in the same range as my NSLinkAttributeNameGamut
T
53

You can use the 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.

Thankless answered 25/4, 2016 at 16:1 Comment(2)
I prefer this one over the selected answer, as I want to use a different color for the cursor/text selection and linksSitin
I have use a UITextView and write above code in viewdidload. but when I write email in text field and enter space color is not changing.Harp
C
36

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];
Carnes answered 15/3, 2010 at 23:53 Comment(5)
This seems extremely hackish for such a simple task.Halden
I know... but I don't know any workaround less hackish. Apple should really think about these little details.Carnes
@Oscar well, i think it's less hackish than Alexanders answer. Still crazy that there's no simple property for such a fundamental functionality.Pondweed
question is about textview not webview!Lysenko
completely agree with "asdasd". Web approach is very expensive, they to see how much ram does it use. for such a simple task use textview and properties Apple gives.Thoma
F
26

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 = [:]
Frasch answered 3/10, 2017 at 18:28 Comment(1)
My String is "Want to learn iOS?" You should visit the best source of free iOS tutorials! Swift checking hello google.com Attributed 9826012345 String testing in on going..." and Default link attribute is required for web link and mobile number. But I want to make a link for string part "Want to learn iOS?" in different color with link..So self.tv.linkTextAttributes = [:] is working only for custom link.Bobbiebobbin
M
19

Swift 5 Answer

Nice and simple

myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]
Marcie answered 6/12, 2019 at 14:45 Comment(0)
P
15

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]];
Paternoster answered 8/11, 2013 at 6:30 Comment(0)
H
6

The tint color can be done in the storyboard also

enter image description here

Hardiness answered 15/4, 2018 at 14:45 Comment(0)
R
3

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="http://www.apple.com" x-apple-data-detectors="true" style="color:white;" x-apple-data-detectors-type="link" x-apple-data-detectors-result="0">http://www.apple.com</a></div>

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

EDIT

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
                                     green:168.0/255.0
                                      blue:224.0/255.0
                                     alpha:1.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];

}
}
Radiosurgery answered 25/6, 2013 at 10:1 Comment(0)
L
1

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;
 font-weight:bold;
 color: #25526e;
}
a.headercopy:link {
 color:#ffffff;
 text-decoration:none;
}
a.headercopy:hover {
 color:#00759B;
 text-decoration:none;
}
a.headercopy:visited {
 color:#ffffff;
 text-decoration:none;
} 
a.headercopy:hover {
 color:#00759B;
 text-decoration:none;
}

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.

Lapel answered 23/10, 2010 at 8:46 Comment(1)
using web views is a bad programming style for native apps.Thoma
F
0

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>
Francoisefrancolin answered 29/6, 2015 at 9:21 Comment(0)
A
-1

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
Apollo answered 2/12, 2019 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.