简体   繁体   English

如何清除 NSMutableAttributedString 中的属性?

[英]How do you clear attributes from NSMutableAttributedString?

How do you set all the attributes in an NSMutableAttributedString to nothing?如何将NSMutableAttributedString中的所有属性设置为NSMutableAttributedString Do you have to enumerate through them and remove them?您是否必须枚举并删除它们?

I don't want to create a new one.我不想创建一个新的。 I am working on the textStorage in NSTextView .我正在处理textStorage中的NSTextView Setting a new string resets the cursor position in NSTextView and fires the delegate.设置新字符串会重置NSTextView的光标位置并触发委托。

You can remove all of the attributes like this:您可以像这样删除所有属性:

NSMutableAttributedString *originalMutableAttributedString = //your string…

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

Note that this uses set Attributes (not add ).请注意,这使用set Attributes (而不是add )。 From the docs:从文档:

These new attributes replace any attributes previously associated with the characters in aRange .这些新属性替换了以前与aRange的字符关联的任何属性。

If you need to do any of it conditionally, you could also enumerate the attributes and remove them one-by-one:如果您需要有条件地执行其中任何一项操作,您还可以枚举属性并一一删除它们:

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

According to the docs this is allowed:根据文档,这是允许的:

If this method is sent to an instance of NSMutableAttributedString , mutation (deletion, addition, or change) is allowed.如果将此方法发送到NSMutableAttributedString的实例,则允许更改(删除、添加或更改)。


Swift 2斯威夫特 2

If string is a mutable attributed string:如果string是可变属性字符串:

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

And if you want to enumerate for conditional removal:如果你想枚举条件删除:

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

swift 4:快速4:

I needed to remove the attributes from a textview in a reusable cell, they were transferred from one cell to another if the other was using the text property, so the text instead being only a text it was with the attributes from the previous cell.我需要从可重用单元格中的 textview 中删除属性,如果另一个单元格使用 text 属性,则它们会从一个单元格转移到另一个单元格,因此文本只是具有前一个单元格属性的文本。 Only this worked for me, inspired by the accepted answer above:受上述公认答案的启发,只有这对我有用:

iOS 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: ""))

How I remove and add attributes of a string in a tableView cellForRowAt indexPath.我如何在 tableView cellForRowAt indexPath 中删除和添加字符串的属性。 Create a new NSMutableAttributedString because it is immutable创建一个新的 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 is immutable therefore you can't do much with an instance of it. NSAttributedString是不可变的,因此您不能对它的实例做太多事情。 One option is to use the string property of NSAttributedString , create a new NSMutableAttributedString with it and apply the desired attributes or make a mutableCopy of your NSAttributedString instance and modify the current attributes for ranges.一种选择是使用NSAttributedStringstring属性,用它创建一个新的NSMutableAttributedString并应用所需的属性或制作NSAttributedString实例的mutableCopy并修改范围的当前属性。

First off, it would have to be an NSMutableAttributedString, as that has the method of removeAttribute(name: String, range: NSRange), and yes it looks like you would have to enumerate them and remove them.首先,它必须是一个 NSMutableAttributedString,因为它有 removeAttribute(name: String, range: NSRange) 的方法,是的,看起来你必须枚举它们并删除它们。

Why not try this approach (Swift):为什么不试试这种方法(Swift):

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

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

相关问题 如何从 NSMutableAttributedString 中删除文本 - How to remove text from NSMutableAttributedString 如何快速初始化一个空的NSMutableAttributedString? - How do I initialize an empty NSMutableAttributedString in swift? iOS - 将所有NSMutableAttributedString属性复制到另一个NSMutableAttributedString - iOS - copy all NSMutableAttributedString attributes to another NSMutableAttributedString 你如何清除UIWebView的缓存? - How do you clear a UIWebView's cache? 更改NSMutableAttributedString的多个部分的属性 - Change attributes of multiple parts of a NSMutableAttributedString 逐个字符地向 NSMutableAttributedString 添加属性 - Add attributes to NSMutableAttributedString character by character 将NSMutableAttributedString的所有属性放入字典 - Get all attributes of a NSMutableAttributedString into a dictionary 从导航堆栈返回后如何清除文本字段框? - How do you clear a textfield box after going back from a navigation stack? 如何使 NSMutableAttributedString 响应设置应用程序中的动态类型文本 - How to make NSMutableAttributedString responsive with dynamic type text from settings app 如何从NSMutableAttributedString(swift)中删除所有特殊字符 - How to remove all special characters from an NSMutableAttributedString (swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM