I'm sure I'm just using it incorrectly, but what I'm doing is I have a NSTextField with a attributed string with a few characters of text in a different font at the end of it. When the user clicks the text field, the text at the end should disappear, and when the user finishes editing their text and removes focus from the text field, the text gets added back to the end of the string they entered.
It's working fine when then user tabs out of the box, or clicks somewhere on the window to remove focus from the textfield. The only time it doesn't work is if they hit the "return" key in the text box. The text still gets added to the end of their string in this case, but it's in the same font as the rest of the string.
Here is the relevant portion of my code. I've verified that both methods are being called in the same sequence both when I tab out of a field and when I hit enter in the field.
- (void) selectText:(id)sender
{
[titleText setStringValue: [NSString stringWithFormat:@"%@", userEditableText]];
}
- (void) textDidEndEditing:(NSNotification *)notification
{
userEditableText = [textField stringValue];
NSString* fullText = [NSString stringWithFormat:@"%@ (%@)", userEditableText, nonUserEditableText];
NSRange range1;
range1.location = 0;
range1.length = [userEditableText length] - ([nonUserEditableText length] + 2);
NSRange range2;
range2.location = range1.length;
range2.length = ([[nonUserEditableText length] length] + 2);
NSRange range3;
range3.location = 0;
range3.length = [fullText length];
NSFont *font = [NSFont fontWithName:@"Arial" size:14.0];
NSMutableDictionary* stringAttributes = [[NSMutableDictionary alloc] init];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:fullText attributes:stringAttributes];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingMiddle];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range1];
[attrString addAttribute:NSFontAttributeName value:font range:range2];
[textField setAttributedStringValue:attrString];
}