在attributeText中添加一个link修饰符后,范围内的文字变得不可见
After adding a link modifier in attributeText, the text in the range becomes invisible
我正在尝试使用 NSAttributedString 创建 link,但是当我向文本添加 link 时,它们变得不可见,下划线仍然可见,如果我单击文本,它会显示我的调试消息,
let attributedText = NSMutableAttributedString(string: "Sample text for test")
attributedText.addAttribute(.link, value: "some url", range: NSMakeRange(0, 5))
attributedText.addAttribute(.underlineStyle, value: 1, range: NSMakeRange(0, 5))
attributedText.addAttribute(.foregroundColor, value: UIColor.blue, range:NSMakeRange(0, 5))
attributedText.addAttribute(.underlineColor, value: UIColor.blue, range: NSMakeRange(0, 5))
myTextView.attributedText = attributedText
myTextView.delegate = self
myTextView.isEditable = false
myTextView.isSelectable = true
我有这样的结果
http://prnt.sc/ojd06v
这是我的代码示例:
let data = Data("My URL stringggg...".utf8)
if let attributedString = try? NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
let foundRange = (attributedString.string as NSString).range(of: "My URL")
if foundRange.location != NSNotFound {
attributedString.addAttribute(NSAttributedString.Key.link, value: "url", range: foundRange)
}
myTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
myTextView.attributedText = attributedString
}
对我有用。
您必须设置 TextView 的 linkTextAttributes
。
我正在尝试使用 NSAttributedString 创建 link,但是当我向文本添加 link 时,它们变得不可见,下划线仍然可见,如果我单击文本,它会显示我的调试消息,
let attributedText = NSMutableAttributedString(string: "Sample text for test")
attributedText.addAttribute(.link, value: "some url", range: NSMakeRange(0, 5))
attributedText.addAttribute(.underlineStyle, value: 1, range: NSMakeRange(0, 5))
attributedText.addAttribute(.foregroundColor, value: UIColor.blue, range:NSMakeRange(0, 5))
attributedText.addAttribute(.underlineColor, value: UIColor.blue, range: NSMakeRange(0, 5))
myTextView.attributedText = attributedText
myTextView.delegate = self
myTextView.isEditable = false
myTextView.isSelectable = true
我有这样的结果 http://prnt.sc/ojd06v
这是我的代码示例:
let data = Data("My URL stringggg...".utf8)
if let attributedString = try? NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
let foundRange = (attributedString.string as NSString).range(of: "My URL")
if foundRange.location != NSNotFound {
attributedString.addAttribute(NSAttributedString.Key.link, value: "url", range: foundRange)
}
myTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
myTextView.attributedText = attributedString
}
对我有用。
您必须设置 TextView 的 linkTextAttributes
。