Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font change on selecting NSTextField

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:

enter image description here

and

enter image description here

Unfortunately when clicking (selecting) this label it appears like this:

enter image description here

and

enter image description here

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


Stack on selection:

enter image description here I believe the problem is that NSCell calls an edit function on mousedown.


The font is also different on selection!! enter image description here


Edit:

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...

like image 650
maxisme Avatar asked Jul 22 '17 16:07

maxisme


1 Answers

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:

Working code

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];
like image 173
Saranjith Avatar answered Oct 14 '22 19:10

Saranjith