Font change on selecting NSTextField

Viewed 1065

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

3 Answers

This behaviour seems to be solved by allowing editing text attributes:

let textField = NSTextField()
textField.allowsEditingTextAttributes = true
textField.isSelectable = true
Related