【问题标题】:OSX NSTextField resets attributedStringValue when text is selected选择文本时,OSX NSTextField 重置属性字符串值
【发布时间】:2016-09-15 07:31:34
【问题描述】:

我为 NSTextField 设置了 attributedStringValue 并带有突出显示的单词:

但是当我通过光标选择文本时,所有突出显示的单词都会得到通常的样式:

我尝试了以下方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidSelected:) name:NSTextViewDidChangeSelectionNotification object:nil];

- (void)controlTextDidSelected:(NSNotification *)notification {
    self.attributedStringValue = _attrString;
}

但在选择时仍然无法正常工作。

如何在选择文本时突出显示单词? 提前致谢。

【问题讨论】:

    标签: xcode macos customization nstextfield nsnotifications


    【解决方案1】:

    我有同样的问题。两件事对我有帮助:
    1. 为整个属性字符串设置字体
    2.允许编辑文本属性。请检查以下代码:

    [self.yourTextField setAllowsEditingTextAttributes: YES];
    [attrString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, [stringValue length])];
    

    【讨论】:

      猜你喜欢
      • 2011-02-25
      • 2016-12-22
      • 2021-09-06
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 2015-04-23
      相关资源
      最近更新 更多