【发布时间】:2016-10-03 11:34:42
【问题描述】:
点击后我需要恢复 NSAttributedString 的可视状态。
我的 NSAttributedString 包含归属于范围的链接。
在此示例中,文本“@user”具有指向“htpp://somesite.com/”的链接:
let text = "Hey @user!"
let attr = NSMutableAttributedString(string: text)
let range = NSRange(location: 4, length: 5)
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orange, range: range)
attr.addAttribute(NSLinkAttributeName, value: "htpp://somesite.com/", range: range)
let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50))
tf.allowsEditingTextAttributes = true
tf.isSelectable = true
tf.stringValue = text
tf.attributedStringValue = attr
效果很好:点击文本字段中的“@user”,它会启动 URL。
但是一旦点击,属性颜色就消失了,取而代之的是这个蓝色并加了下划线:
单击字符串后,我找不到恢复原始颜色的解决方案(或完全避免这种自动更改)。
我见过this 和this,但没有实际的解决方案,我无法将指向的库集成到我的项目中(实际上,我真的很想不必导入任何库)。
请注意,我现有的代码是用 Swift 编写的,但我可以使用 Objective-C 解决方案。
【问题讨论】:
标签: macos cocoa nsattributedstring nstextfield nstextview