如何从 NSMutableAttributedString 中清除属性?

问题描述 投票:0回答:6

如何将

NSMutableAttributedString
中的所有属性设置为空?您是否必须枚举它们并删除它们?

我不想创建一个新的。我正在

textStorage
中的
NSTextView
工作。设置新字符串会重置
NSTextView
中的光标位置并触发委托。

ios objective-c swift nsattributedstring
6个回答
22
投票

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

NSMutableAttributedString *originalMutableAttributedString = //your string…

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

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

这些新属性将替换之前与

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];
                                                        }];
                                                    }];

根据文档,这是允许的:

如果将此方法发送到

NSMutableAttributedString
的实例,则允许突变(删除、添加或更改)。


斯威夫特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)
    }
}

5
投票

迅捷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: ""))

2
投票

如何在 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
    }

1
投票

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


0
投票

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

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

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

0
投票
extension NSMutableAttributedString {
    func attributesClear() {
        let range = NSRange(location: 0, length: self.length)
        
        self.attributes.keys
            .forEach{
                self.removeAttribute($0, range: range)
            }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.