I've got an Label (NSTextField) in IB that's bound to a controller. The controller, on awakeFromNIB, sets the attributedStringValue of the label to contain some coloured text and a link or two.
When you see the label it contains the correct string value, but some of the formatting is lost - until you click on the label, and it updates to contain the correct formatting.
I'm using 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'll see the string coloured red and in the default Label font. Then when you click on the label the font changes size and face and the link magically renders.
Any ideas on how to get the string to render correctly the first time?
beginEditingandendEditingwhen you are constructing an attributed string. These methods are only for use when the string is being monitored for changes, such asNSTextStoragefor anNSTextView. - Rob KenigerattributedStringValuebinding actually bound to anything? - Rob Keniger[textField setNeedsDisplay:YES]to force a redraw? - Rob Keniger