用另一个 NSAttributedString 替换 NSAttributedString 的子字符串 [英] Replace substring of NSAttributedString with another NSAttributedString
问题描述
我想用另一个 NSAttributedString
替换 NSAttributedString
的子字符串(例如 @"replace"
).
I want to replace a substring (e.g. @"replace"
) of an NSAttributedString
with another NSAttributedString
.
我正在寻找与 NSString
的 stringByReplacingOccurrencesOfString:withString:
用于 NSAttributedString
.
I am looking for an equivalent method to NSString
's stringByReplacingOccurrencesOfString:withString:
for NSAttributedString
.
推荐答案
将您的属性字符串转换为
NSMutableAttributedString
的实例.
可变属性字符串具有 mutableString
属性.根据文档:
The mutable attributed string has a mutableString
property. According to the documentation:
接收器跟踪对此字符串的更改并使其属性映射保持最新."
"The receiver tracks changes to this string and keeps its attribute mappings up to date."
因此您可以使用生成的可变字符串来执行与 replaceOccurrencesOfString:withString:options:range:
的替换.
So you can use the resulting mutable string to execute the replacement with replaceOccurrencesOfString:withString:options:range:
.
这篇关于用另一个 NSAttributedString 替换 NSAttributedString 的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!