为什么当我单击链接文本时,带有属性字符串的NSTextField会自动布局?

时间:2019-06-25 10:50:00

标签: nsattributedstring nstextfield

环境:Xcode 10.2,macOS Mojave版本10.14.5

我用如下属性字符串创建一个NSTextField:

let name = "Someone"
let link = "http://example.com/people/\(name)"
let attributedString = NSMutableAttributedString(string: name)

// link without underline
attributedString.addAttributes([.link : link, .underlineStyle : 0], range: NSRange(location: 0, length: name.count))

// bind UI
let label = NSTextField(frame: CGRect(x: 10, y: 10, width: 100, height: 30))
label.allowsEditingTextAttributes = true
label.isSelectable = true
label.attributedStringValue = attributedString

当用户界面成功显示时,我单击链接,文本区域将再次被布局并缩小文本。我错过了什么吗?

NSTextField's AttributedString Shrink

1 个答案:

答案 0 :(得分:0)

好,最后。

我自己设置属性字符串的字体。

// link without underline
attributedString.addAttributes([.link : link,
                                .underlineStyle : 0,
                                .font: NSFont.systemFont(ofSize: 13)] // same as textField.

然后问题解决了。