3

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.

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

Any suggestions how to prevent this?

MechMK1
  • 3,278
  • 7
  • 37
  • 55
  • Perhaps some one of the NS-Objects involved automatically change text attributes unless handled/told-otherwise? I would start by implementing the NSTextViewDelegate, and using some `NSLog`s in the delegate method:`textView:shouldChangeTextInRange:replacementString:`. More info on the method: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSTextViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSTextViewDelegate/textView:clickedOnLink:atIndex: – AlexFZ Aug 14 '11 at 18:27
  • Gah, just noticed that you are using an `NSTextField` instead of an `NSTextView`. Sorry about that, I should have read more carefully. Anyways, my suggestion would still be to look into the listener/delegate methods of all objects involved, and use print statements to see when the text is actually changing. That should give you an idea of where to start searching for how to stop that change from occuring. – AlexFZ Aug 14 '11 at 18:32

2 Answers2

2

Add a specific NSFont to your NSAttributedString

[attrString addAttribute:@"NSFont" value:[NSFont systemFontOfSize:10.0] range:range];
Christoph R
  • 928
  • 6
  • 10
0

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.

Francis McGrew
  • 7,264
  • 1
  • 33
  • 30