Ios 更改NSMutableAttributeString中链接的颜色
我有以下代码,但我的链接总是蓝色的。我怎样才能知道它们的颜色Ios 更改NSMutableAttributeString中链接的颜色,ios,iphone,nsattributedstring,nsmutableattributedstring,Ios,Iphone,Nsattributedstring,Nsmutableattributedstring,我有以下代码,但我的链接总是蓝色的。我怎样才能知道它们的颜色 [_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)]; [_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(positio
[_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)];
_字符串是一个NSMutableAttributedString,位置和长度可以正常工作
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }];
目标-C
这将使带下划线的白色文本可单击。为代码选择必要的属性并使用它
要使字符串中包含可单击链接,请执行以下操作:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Click " attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15]}];
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }];
[string appendAttributedString:attributedString];
因此,您将得到字符串“单击此处”,而“此处”将是一个链接。您可以为每个字符串设置不同的样式。Swift
更新为Swift 4.2
将linktexttributes
与UITextView一起使用
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};
在这方面:
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
目标-C
将linktexttributes
与UITextView一起使用
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};
资料来源:
和来自:
链接颜色是标签/文本视图的着色颜色。因此,可以通过更改视图的着色颜色来更改它。但是,如果您希望在同一视图中使用不同的链接颜色,则此操作无效。Swift
对于swift3.0
override func viewDidLoad() {
super.viewDidLoad()
let linkAttributes = [
NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
] as [String : Any]
let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
textview.delegate = self
textview.attributedText = attributedString
textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
textview.textColor = UIColor.white
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return true
}
我使用了这个:我已经能够更改链接颜色,但是如何更改当您一直按链接时显示的颜色呢?很好,但在swift 4.4中使用textView.linktexttributes=[NSAttributedString.Key.foregroundColor.rawValue:UIColor.green]
在iOS 12上对我有效。适用于UITextView在Xcode 11.2.1中不起作用,UILabel的Swift 5@crcalin@ios只需使用UITextView,它就可以使用它