我可以在UITextView上更改自动检测到的链接的颜色吗?

Lea*_*ves 112 iphone uitextview uicolor iphone-sdk-3.0

我有一个UITextView检测到电话号码和链接,但这会覆盖我fontColor并将其更改为blueColor.有没有办法格式化自动检测链接的颜色,或者我应该尝试这个功能的手动版本?

sto*_*onk 158

在iOS 7,您可以设置tintColorUITextView.它会影响链接颜色以及光标线和选定的文本颜色.

iOS 7还为UITextView被调用添加了一个新属性,linkTextAttributes它可以让您完全控制链接样式.

  • 我在这个答案中给出了进一步的解释:http://stackoverflow.com/a/21027340/1202222 (2认同)

Lea*_*ves 39

我没有使用UITextView,而是使用了UIWebView并启用了"自动检测链接".要更改链接颜色,只需为标记创建常规CSS.

我使用过这样的东西:

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];
Run Code Online (Sandbox Code Playgroud)

  • 对于这么简单的任务来说,这似乎非常苛刻. (12认同)

Gru*_*bas 34

您可以使用UIAppearance协议对所有文本视图应用更改:

Swift 4.x:

UITextView.appearance().linkTextAttributes = [ .foregroundColor: UIColor.red ]
Run Code Online (Sandbox Code Playgroud)

Swift 3.x:

UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.red ]
Run Code Online (Sandbox Code Playgroud)

Swift 2.x:

UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.redColor() ]
Run Code Online (Sandbox Code Playgroud)

Objective-C的:

[UITextView appearance].linkTextAttributes = @{ NSForegroundColorAttributeName : UIColor.redColor };
Run Code Online (Sandbox Code Playgroud)

外观UITextView没有记录,但效果很好.

请记住UIAppearance:

当视图进入窗口时,iOS会应用外观更改,它不会更改已在窗口中的视图的外观.要更改当前在窗口中的视图的外观,请从视图层次结构中删除该视图,然后将其放回.

换句话说:调用此代码init(),或者init(coder:)方法改变UI对象外观,但调用in loadView()viewDidLoad()viewController 不会.
如果要为整个应用程序设置外观,application(_:didFinishLaunchingWithOptions:)是调用此类代码的好地方.


小智 12

您可以通过以下方式更改TextView中的超链接颜色:

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]];
Run Code Online (Sandbox Code Playgroud)


mat*_*att 11

UITextView的问题linkTextAttributes在于它适用于所有自动检测到的链接.如果您希望不同链接具有不同属性,该怎么办?

事实证明有一个技巧:将链接配置为文本视图的属性文本的一部分,并将其设置linkTextAttributes为空字典.

这是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 = [:]
Run Code Online (Sandbox Code Playgroud)


小智 9

斯威夫特 5 个回答

好看又简单

myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]
Run Code Online (Sandbox Code Playgroud)


Rya*_*ner 5

色调颜色也可以在故事板中完成

在此处输入图片说明