cocoa - 删除 NSTextView 文本选择的属性

标签 cocoa nsattributedstring nstextview nsrange

我有一个通过界面生成器添加的 NSTextView 对象。我可以更改已选择的文本段的背景颜色,如下所示。

- (void)emphasizeText {
    NSColor *c = [colorWell.color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; //colorWell is an NSColorWell object that has been added through interface builder
    NSRange selection = textView1.selectedRange; //textView1 is an NSTextView object that has been added through interface builder
    [textView1.textStorage addAttributes:@{NSBackgroundColorAttributeName:c} range:selection];
}

但是,我无法从所选文本段中删除添加的属性 (NSBackgroundColorAttributeName)。引用this topic ,我有以下代码。

- (void)deemphasizeText {
    NSMutableAttributedString *aStr = [textView1.attributedString mutableCopy];
    NSRange selection = textView1.selectedRange;
    [aStr enumerateAttribute:NSBackgroundColorAttributeName inRange:selection options:0 usingBlock:^(id value,NSRange range,BOOL *stop) {
        if (value) {
            [aStr removeAttribute:NSBackgroundColorAttributeName range:range];
        }
    }];
}

尽管应用程序不会崩溃,但没有任何变化。我做了几种变体,例如

- (void)deemphasizeText {
    NSRange selection = textView1.selectedRange;
    NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithAttributedString:(NSMutableAttributedString *)[textView1.textStorage attributedSubstringFromRange:selection]];
    [aStr enumerateAttribute:NSBackgroundColorAttributeName inRange:NSMakeRange(0,aStr.length) options:0 usingBlock:^(id value,NSRange range,BOOL *stop) {
        if (value) {
            [aStr removeAttribute:NSBackgroundColorAttributeName range:range];
        }
    }];
}

同样,选定的片段不会改变。我做错了什么?

非常感谢。

最佳答案

首先,您的 -deemphasizeText 方法显式使用文本存储可变字符串的副本。人们不会指望修改该副本来修改原始版本。

其次,您应该通过调用 -beginEditing-endEditing 将文本存储的所有修改括起来。事实上,您确实应该在更改周围的 TextView 上调用 -shouldChangeTextInRange:replacementString:-didChangeText

最后,无需枚举属性字符串并仅从其存在的范围中删除该属性。只需删除整个选择范围的属性即可。

所以:

- (void)emphasizeText {
    NSColor *c = [colorWell.color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
    NSRange selection = textView1.selectedRange;
    if ([textView1 shouldChangeTextInRange:selection replacementString:nil])
    {
        NSTextStorage* storage = textView1.textStorage;
        [storage beginEditing];
        [storage addAttributes:@{NSBackgroundColorAttributeName:c} range:selection];
        [storage endEditing];
        [textView1 didChangeText];
    }
}

- (void)deemphasizeText {
    NSRange selection = textView1.selectedRange;
    if ([textView1 shouldChangeTextInRange:selection replacementString:nil])
    {
        NSTextStorage* storage = textView1.textStorage;
        [storage beginEditing];
        [storage removeAttribute:NSBackgroundColorAttributeName range:selection];
        [storage endEditing];
        [textView1 didChangeText];
    }
}

其他需要考虑的事情:

  • 调整选择范围以适合您正在进行的修改。调用-rangeForUserCharacterAttributeChange;检查其位置是否为 NSNotFound,如果是则中止;检查它是否与当前的 selectedRange 不同,如果是,则将 selectedRange 设置为匹配;然后在该范围内进行操作。
  • 使用 -rangesForUserCharacterAttributeChange-shouldChangeTextInRanges:replacementStrings: 支持多个选择范围。

关于cocoa - 删除 NSTextView 文本选择的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34576809/

相关文章:

ios - NSManagedContext 需要很长时间才能持久

objective-c - Cocoa 中的本地对象作用域和内存管理

xml - 在 Cocoa 中解析 XML

ios - UITextView中触发超链接文本点击和普通文本点击

ios - UITextView 保存属性文本

objective-c - 从工具栏项中删除焦点

ios - 表情符号支持 NSAttributedString 属性(字距调整/段落样式)

objective-c - NSTextView 暂时禁用自动拼写更正

cocoa - 查找选定的 NSTextView

objective-c - 如何在 NSTextView 中设置字体?