我正在使用 uilabel 并使用 NSAttributedString 添加链接
let attributedText = NSMutableAttributedString(string: "http://www.google.com")
attributedText.addAttributes([NSAttributedString.Key.link: "https://www.google.com"], range: NSRange(location: 0, length: 21))
attributedText.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], range: NSRange(location: 0, length: 21))
label.attributedText = attributedText
label.tintColor = UIColor.red
如何使链接变为除 UIKit 框架提供的其他默认蓝色链接颜色之外的其他颜色。
注意:我不想使用 UITextView 或 UIWebView
最佳答案
let attributedText = "http://www.google.com"
let multipleAttributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue ]
let attributeString = NSAttributedString(string: attributedText, attributes: multipleAttributes)
// set attributed text on a UILabel
label.attributedText = attributeString
关于swift - iOS 使用 NSAttributedString 时更改 uilabel 链接的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740414/