用另一个 NSAttributedString 替换 NSAttributedString 的子字符串 [英] Replace substring of NSAttributedString with another NSAttributedString

查看:31
本文介绍了用另一个 NSAttributedString 替换 NSAttributedString 的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个 NSAttributedString 替换 NSAttributedString 的子字符串(例如 @"replace").

I want to replace a substring (e.g. @"replace") of an NSAttributedString with another NSAttributedString.

我正在寻找与 NSStringstringByReplacingOccurrencesOfString:withString: 用于 NSAttributedString.

I am looking for an equivalent method to NSString's stringByReplacingOccurrencesOfString:withString: for NSAttributedString.

推荐答案

  1. 将您的属性字符串转换为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆