在一个NSAttributedString字母中,一系列字母具有链接属性和自定义颜色属性.
在带有Swift 2的Xcode 7中,它可以工作:
在带有Swift 3的Xcode 8中,链接的自定义属性颜色始终被忽略(截图中应为橙色).
这是测试代码.
Swift 2,Xcode 7:
import Cocoa
import XCPlayground
let text = "Hey @user!"
let attr = NSMutableAttributedString(string: text)
let range = NSRange(location: 4, length: 5)
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orangeColor(), range: range)
attr.addAttribute(NSLinkAttributeName, value: "http://somesite.com/", range: range)
let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50))
tf.allowsEditingTextAttributes = true
tf.selectable = true
tf.stringValue = text
tf.attributedStringValue = attr
XCPlaygroundPage.currentPage.liveView = tf
Run Code Online (Sandbox Code Playgroud)
Swift 3,Xcode 8:
import Cocoa
import PlaygroundSupport
let text …Run Code Online (Sandbox Code Playgroud) 我想更改 UILabel 中链接的颜色。我发现了很多关于如何为 UITextView 执行此操作的过去问题,以及过去在 Obj-C 中有答案的问题(但无法将这些转换为 Swift,因为 Obj-c 中确实存在的属性不再执行,例如 NSMutableAttributedString .linkTextAttributes 例如)。但是我找不到如何为 UILabel 和 Swift 4 执行此操作。