11

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

Community
  • 1
  • 1
maxisme
  • 3,974
  • 9
  • 47
  • 97

3 Answers3

1

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];
Saranjith
  • 11,242
  • 5
  • 69
  • 122
0

To fix this I made a custom NSTextField where on select it updates like this:

- (void)textViewDidChangeSelection:(NSNotification *)a{
    [self setNeedsDisplay:YES];
    [self setNeedsLayout:YES];
    [self.superview setNeedsLayout:YES];
    [self.superview setNeedsDisplay:YES];
}

but still acts funny some times

maxisme
  • 3,974
  • 9
  • 47
  • 97
0

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

let textField = NSTextField()
textField.allowsEditingTextAttributes = true
textField.isSelectable = true
vicegax
  • 4,709
  • 28
  • 37