【问题标题】:Font change on selecting NSTextField选择 NSTextField 时的字体更改
【发布时间】:2017-07-22 16:12:32
【问题描述】:

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

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;

结果是这样的:

不幸的是,当单击(选择)此标签时,它看起来像这样:

这有点大胆和像素化的角落。这发生在许多其他具有不同字符串、大小和颜色的标签上。我该如何解决?!

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


选择堆栈:

我认为问题在于 NSCell 在 mousedown 上调用了一个编辑函数。


字体在选择上也不同!!


编辑:

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

【问题讨论】:

  • 您可以通过不将背景设置为clearColor来修复它。
  • 你是最棒的。如果你在我写的另一个问题上回答了我的赏金!确保将其写为答案。
  • 不用等待。它没有用......什么?!过了一会儿。
  • 啊,它有时会起作用。
  • 你在uilabel中启用了用户交互??

标签: objective-c macos nsattributedstring nstextfield


【解决方案1】:

将此行添加到您的代码中。

NSTextView *textEditor = (NSTextView *)[[[NSApplication sharedApplication] keyWindow] fieldEditor:YES forObject:title_field];

[textEditor setSelectedTextAttributes:noNotificationsAttrs];

从而在选择时将属性添加到与窗口关联的 NSTextView。

您现在得到的内容(如选择后的粗体和字体更改)将被覆盖。

编辑:

工作代码

已将 NSForegroundColorAttributeName 更改为 NSBackgroundColorAttributeName_grey[NSColor clearColor]

NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
                                      NSParagraphStyleAttributeName,
                                      [NSFont fontWithName:@"Raleway-Bold" size:30],
                                      NSFontAttributeName,
                                      [NSColor clearColor],
                                      NSBackgroundColorAttributeName,
                                      nil];

【讨论】:

  • No visible @interface for 'MyLabel' declares the selector 'setSelectedTextAttributes:'
  • 什么是InputWindow
  • InputTextField?
  • 仍然发生 :(
  • 查看我在主要问题中的评论以查看所有代码!我相信它与嵌套在 nsscroll 中的事实有关
【解决方案2】:

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

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

但有时仍然表现得很有趣

【讨论】:

    【解决方案3】:

    这种行为似乎可以通过允许编辑文本属性来解决:

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

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2012-06-22
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多