如何清除 NSMutableAttributedString 中的属性?

How do you clear attributes from NSMutableAttributedString?

如何将 NSMutableAttributedString 中的所有属性设置为空?是不是一定要枚举一遍再去掉?

我不想创建一个新的。我正在研究 NSTextView 中的 textStorage。设置新字符串会重置 NSTextView 中的光标位置并触发委托。

NSAttributedString 是不可变的,因此您不能对它的实例做太多事情。一种选择是使用 NSAttributedStringstring 属性,用它创建一个新的 NSMutableAttributedString 并应用所需的属性,或者制作一个 mutableCopy 你的 NSAttributedString实例并修改范围的当前属性。

您可以像这样删除所有属性:

NSMutableAttributedString *originalMutableAttributedString = //your string…

NSRange originalRange = NSMakeRange(0, originalMutableAttributedString.length);
[originalMutableAttributedString setAttributes:@{} range:originalRange];

请注意,这使用了 set 属性(而不是 add)。来自文档:

These new attributes replace any attributes previously associated with the characters in aRange.

如果您需要有条件地执行其中任何一项,您还可以枚举属性并逐一删除它们:

[originalMutableAttributedString enumerateAttributesInRange:originalRange
                                                    options:kNilOptions
                                                 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
                                                        [attrs enumerateKeysAndObjectsUsingBlock:^(NSString *attribute, id obj, BOOL *stop) {
                                                            [originalMutableAttributedString removeAttribute:attribute range:range];
                                                        }];
                                                    }];

根据文档,这是允许的:

If this method is sent to an instance of NSMutableAttributedString, mutation (deletion, addition, or change) is allowed.


Swift 2

如果 string 是可变属性字符串:

string.setAttributes([:], range: NSRange(0..<string.length))

如果您想枚举以进行有条件的删除:

string.enumerateAttributesInRange(NSRange(0..<string.length), options: []) { (attributes, range, _) -> Void in
    for (attribute, object) in attributes {
        string.removeAttribute(attribute, range: range)
    }
}

首先,它必须是一个 NSMutableAttributedString,因为它有 removeAttribute(name: String, range: NSRange) 方法,是的,看起来你必须枚举它们并删除它们。

为什么不尝试这种方法 (Swift):

let s1 = <some NSAttributedString>
var s2 = NSMutableAttriutedString(string: s1.string())

swift 4:

我需要从可重复使用的单元格中的文本视图中删除属性,如果另一个单元格正在使用文本,它们会从一个单元格转移到另一个单元格 属性,因此文本只是一个文本与前一个单元格的属性。 只有这对我有用,灵感来自上面接受的答案:

iOS

let attr = NSMutableAttributedString(attributedString: (cell.textView?.attributedText)!)
    let originalRange = NSMakeRange(0, attr.length)
    attr.setAttributes([:], range: originalRange)
    cell.textView?.attributedText = attr
    cell.textView?.attributedText = NSMutableAttributedString(string: "", attributes: [:])
    cell.textView?.text = ""

macOS

textView.textStorage?.setAttributedString(NSAttributedString(string: ""))

我如何在 tableView cellForRowAt indexPath 中删除和添加字符串的属性。 创建一个新的 NSMutableAttributedString 因为它是不可变的

// strike through on text
    let attributeString =  NSMutableAttributedString(string: "your string")
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))

    let removedAttributeString = NSMutableAttributedString(string: "your string")
    removedAttributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))

    if (item.done == true) {
        // applies strike through on text
        cell.textLabel?.attributedText = attributeString

    } else {
        // removes strike through on text
        cell.textLabel?.attributedText = removedAttributeString
    }