I am wanting to create a simple label with an attributed string. I do this like this:
NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:@"Raleway-Bold" size:30],
NSFontAttributeName,
_grey,
NSForegroundColorAttributeName,
nil];
NSMutableAttributedString *noNotificationsString =
[[NSMutableAttributedString alloc] initWithString:@"No Notifications"
attributes:noNotificationsAttrs];
NSTextField* title_field = [[NSTextField alloc] initWithFrame:
CGRectMake(
0,
0,
200,
200
)
];
[title_field setWantsLayer:true];
[title_field setSelectable:YES];
[title_field setAllowsEditingTextAttributes:true];
[title_field setAttributedStringValue:noNotificationsString];
[title_field setEditable:false];
[title_field setBordered:false];
title_field.tag = 1;
Which turns out like this:
and
Unfortunately when clicking (selecting) this label it appears like this:
and
Which is a bit bolder and pixelated around the corners. This is happening with lots of other labels with different strings, sizes and colours. How can I fix it?!
Please note these labels are nested inside nsscrollview
-> nstableview
-> viewForTableColumn
I believe the problem is that NSCell calls an edit function on mousedown.
The font is also different on selection!!
Interestingly if I remove wantslayer:YES
from the (2) parent view it does not do this. But they both need wantslayer or else I can't have curved corners etc...
Add this line to your code.
NSTextView *textEditor = (NSTextView *)[[[NSApplication sharedApplication] keyWindow] fieldEditor:YES forObject:title_field];
[textEditor setSelectedTextAttributes:noNotificationsAttrs];
Thus adding the attributes to NSTextView associated with window while selection.
What you're getting now like bold and font changes after selection will get overrided.
Edit:
Have changed NSForegroundColorAttributeName
to NSBackgroundColorAttributeName
and _grey
to [NSColor clearColor]
NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:@"Raleway-Bold" size:30],
NSFontAttributeName,
[NSColor clearColor],
NSBackgroundColorAttributeName,
nil];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With