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进行了更新
使用linkTextAttributes带UITextView
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)
使用linkTextAttributes带UITextView
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)
小智 38
链接颜色是标签/ textView的着色颜色.因此,您可以通过更改视图的色调颜色来更改它.但是,如果您想在同一视图中使用不同的链接颜色,则无法使用此功能.
迅速
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)
| 归档时间: |
|
| 查看次数: |
36592 次 |
| 最近记录: |