NSTextField with URL-style format

2019-02-18 22:35发布

问题:

I've been trying to let an NSTextField recognize URL's and in general my code works. I followed Apple's instructions on this page on how to extend NSAttributedString with hyperlinkFromString:withUrl and my URL's get displayed quite well. However, when I click the link, the text-format changes into some smaller text style and probably another font. I've got two images below with two NSLabels containing the same URL's. The first shows their default state, the second how they look like after the first has been clicked.

http://i.stack.imgur.com/SAVa0.png
http://i.stack.imgur.com/rC4Hq.png

Any suggestions how to prevent this?

回答1:

Add a specific NSFont to your NSAttributedString

[attrString addAttribute:@"NSFont" value:[NSFont systemFontOfSize:10.0] range:range];


回答2:

Sigh... I tried to get this working myself and ended up having to use a NSTextView. Another option is the custom NSTextField code here which works quite well.