0
votes

Environment: Xcode 10.2, macOS Mojave Version 10.14.5

I create an NSTextField with an attributed string like below:

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

When the UI shows successfully, I click the link, the text area will be layout again and text shrink. Do I have missed something?

NSTextField's AttributedString Shrink

1
I guess the attributed string's layout is calculated when the user clicks the link. But why attributed string does not use the NSTextField's default font? - Chen

1 Answers

0
votes

At last, I solved this problem by setting up the font of the attributed string. Code like as below works.

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