繁体   English   中英

如何清除 NSMutableAttributedString 中的属性?

[英]How do you clear attributes from NSMutableAttributedString?

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

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

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

NSMutableAttributedString *originalMutableAttributedString = //your string…

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

请注意,这使用set Attributes (而不是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)
    }
}

快速4:

我需要从可重用单元格中的 textview 中删除属性,如果另一个单元格使用 text 属性,则它们会从一个单元格转移到另一个单元格,因此文本只是具有前一个单元格属性的文本。 受上述公认答案的启发,只有这对我有用:

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 = ""

苹果系统

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
    }

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM