2016-09-15 80 views
1

我设置为attributedStringValue用的NSTextField强调的话:enter image description hereOSX的NSTextField重置attributedStringValue当文本被选中

,但是当我选择由光标文字各种高亮的话得到一贯的风格:enter image description here

我尝试以下操作:

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

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

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

如何在选择文本时突出显示单词? 在此先感谢。

回答

2

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

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