更改NSMutableAttributedString中链接的颜色

cdu*_*dub 64 iphone nsattributedstring ios nsmutableattributedstring

我有以下代码,但我的链接始终是蓝色的.我如何塑造它们的颜色?

[_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)];
[_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(position, length)];
[_string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(position, length)];
Run Code Online (Sandbox Code Playgroud)

_string是一个NSMutableAttributedString,位置和长度工作正常.

Sur*_*gch 151

迅速

针对Swift 4.2进行了更新

使用linkTextAttributesUITextView

textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
Run Code Online (Sandbox Code Playgroud)

在上下文中:

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 is a UITextView
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)

Objective-C的

使用linkTextAttributesUITextView

textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};
Run Code Online (Sandbox Code Playgroud)

来源:这个答案

并从这篇文章:

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)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;
Run Code Online (Sandbox Code Playgroud)

  • 完美,但在 swift 4.4 中使用 `textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor.rawValue: UIColor.green]` (2认同)

小智 38

链接颜色是标签/ textView的着色颜色.因此,您可以通过更改视图的色调颜色来更改它.但是,如果您想在同一视图中使用不同的链接颜色,则无法使用此功能.

  • 这对于iOS 9中的"UILabel"不起作用. (13认同)
  • 不适用于 UILabel @crcalin 的 Xcode 11.2.1、Swift 5 (10认同)

Das*_*oga 7

迅速

 let str = "By using this app you agree to our Terms and Conditions and Privacy Policy"
 let attributedString = NSMutableAttributedString(string: str)
 var foundRange = attributedString.mutableString.rangeOfString("Terms and Conditions")

 attributedString.addAttribute(NSLinkAttributeName, value: termsAndConditionsURL, range: foundRange)
 foundRange = attributedString.mutableString.rangeOfString("Privacy Policy")
 attributedString.addAttribute(NSLinkAttributeName, value: privacyURL, range: foundRange)
 policyAndTermsTextView.attributedText = attributedString
 policyAndTermsTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.blueColor()]
Run Code Online (Sandbox Code Playgroud)