Schnell
Aktualisiert für Swift 4.2
Verwenden Sie linkTextAttributes
mit einemUITextView
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
Und im Kontext:
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
Ziel c
Verwenden Sie linkTextAttributes
mit einemUITextView
textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]}
Quelle: diese Antwort
Und aus diesem Beitrag :
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;