选择NSTextField时字体更改

max*_*sme 11 macos objective-c nstextfield nsattributedstring

我想创建一个带有属性字符串的简单标签.我是这样做的:

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;
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

在此输入图像描述

在此输入图像描述

不幸的是,当点击(选择)此标签时,它显示如下:

在此输入图像描述

在此输入图像描述

在角落周围有点大胆和像素化.这种情况正在发生,许多其他标签具有不同的字符串,大小和颜色.我该怎么办呢?!

请注意,这些标签嵌套在nsscrollview- > nstableview- >中viewForTableColumn


堆叠选择:

在此输入图像描述 我相信问题是NSCell在mousedown上调用了一个编辑功能.


选择的字体也不同!! 在此输入图像描述


编辑:

有趣的是,如果我wantslayer:YES从(2)父视图中删除它不会这样做.但他们都需要想要层,否则我就不能有弯角......

max*_*sme 0

为了解决这个问题,我制作了一个自定义 NSTextField,在选择它时它会像这样更新:

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

但有时还是表现得很搞笑