[英]How to remove all special characters from an NSMutableAttributedString (swift)
我有一个NSMutableAttributed字符串attributedString
,其中我正试图删除所有特殊字符“ @”。 但是,当我尝试更改attributedString的字符串值时,它给出了该字符串是仅获取属性的错误。
是,因为错误表明NSMutableAttributedString
string
属性为只读,因此无法设置。 相反,您可以使用属性字符串的mutableString
属性,并根据需要进行更改。 您的代码应类似于以下内容:
attributedString.mutableString.replaceOccurrences(of: "@", with: "", options: NSString.CompareOptions(rawValue: 0), range: NSMakeRange(0, attributedString.mutableString.length));
并根据文档:
接收者跟踪对此字符串的更改,并保持其属性映射为最新。
因此,您也不会丢失任何样式信息!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.