迅速
为Swift 4.2更新
使用linkTextAttributes
带UITextView
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
在上下文中:
let attributedString = NSMutableAttributedString(string: "The site is www.google.com.")
let linkRange = (attributedString.string as NSString).range(of: "www.google.com")
attributedString.addAttribute(NSAttributedString.Key.link, value: "https://www.google.com", range: linkRange)
let linkAttributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.green,
NSAttributedString.Key.underlineColor: UIColor.lightGray,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString
目标C
使用linkTextAttributes
带UITextView
textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]}
资料来源:这个答案
从这篇文章:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
value:@"username://marcelofabri_"
range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
NSUnderlineColorAttributeName: [UIColor lightGrayColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
textView.linkTextAttributes = linkAttributes;
textView.attributedText = attributedString;
textView.delegate = self;