I have a Label (NSTextField) in IB that is connected to a controller. The controller, on awakeFromNIB, sets the attribatedStringValue attribute of the label to contain colored text and a link or two.
When you see a label, it contains the correct string value, but part of the formatting is lost - until you click on the label and it is updated to contain the correct formatting.
I use this code to set the value:
[self.testTextField setAllowsEditingTextAttributes:YES]; [self.testTextField setSelectable:YES]; NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:@"hit this "]; [linkString beginEditing]; NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"link"]; NSRange range = NSMakeRange(0, [attrString length]); [attrString addAttribute:NSLinkAttributeName value:[[NSURL URLWithString:@"http://google.com"] absoluteString] range:range]; [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlinePatternDot] range:range]; [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blackColor] range:range]; [linkString appendAttributedString:attrString]; [linkString appendAttributedString:[[NSAttributedString alloc] initWithString:@" to search"]]; [linkString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, [linkString length])]; [linkString endEditing]; [self.testTextField setAttributedStringValue:linkString];
Based on this example, you will see that the string is colored red and the default font is Label. Then, when you click on the label, the font changes size and face, and the link magically displays.
Any ideas on how to wrap a line for the first time?
source share