簡體   English   中英

如何從NSMutableAttributedString(swift)中刪除所有特殊字符

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM