将 NSAttributedString 的子字符串替换为另一个 NSAttributedString
我想用另一个 NSAttributedString
替换 NSAttributedString
的子字符串(例如 @"replace"
)。
我正在寻找与 NSString
的 stringByReplacingOccurrencesOfString:withString:
为NSAttributedString
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
将属性字符串转换为
NSMutableAttributedString
的实例。可变属性字符串有一个
mutableString
属性。根据文档:<块引用>
“接收器跟踪此字符串的更改并保持其属性映射最新。”
因此,您可以使用生成的可变字符串来执行替换
replaceOccurrencesOfString:withString:options:range:
。Convert your attributed string into an instance of
NSMutableAttributedString
.The mutable attributed string has a
mutableString
property. According to the documentation:So you can use the resulting mutable string to execute the replacement with
replaceOccurrencesOfString:withString:options:range:
.就我而言,以下方法是唯一的(在iOS9上测试):
当然,找到另一种更好的方法会很高兴。
In my case, the following way was the only (tested on iOS9):
Of course it will be nice to find another better way.
以下是如何更改 NSMutableAttributedString 的字符串,同时保留其属性:
Swift:
Objective-C:
Here is how you can change the string of NSMutableAttributedString, while preserving its attributes:
Swift:
Objective-C:
斯威夫特4:
更新了 Swift 4 的 sunkas 优秀解决方案,并封装在“扩展”中。只需将其夹入 ViewController(在类之外)并使用它即可。
Swift 4:
Updated sunkas excellent solution to Swift 4 and wrapped in "extension". Just clip this into your ViewController (outside the class) and use it.
对于 Swift 4 和 iOS 11,您可以使用以下 2 种方法之一来解决您的问题。
#1.使用
NSMutableAttributedString
replaceCharacters(in:with:)
方法NSMutableAttributedString
有一个名为replaceCharacters(in:with:)
。replaceCharacters(in:with:)
具有以下声明:下面的 Playground 代码展示了如何使用
replaceCharacters(in:with:)
将NSMutableAttributedString
实例的子字符串替换为新的NSMutableAttributedString
实例:#2。使用
NSMutableString
replaceOccurrences(of:with:options:range:)
方法NSMutableString
有一个名为replaceOccurrences(of:with:options:range:)
。replaceOccurrences(of:with:options:range:)
具有以下声明:下面的 Playground 代码展示了如何使用
replaceOccurrences(of:with:options:range:)
将NSMutableAttributedString
实例的子字符串替换为新的NSMutableAttributedString实例:
With Swift 4 and iOS 11, you can use one of the 2 following ways in order to solve your problem.
#1. Using
NSMutableAttributedString
replaceCharacters(in:with:)
methodNSMutableAttributedString
has a method calledreplaceCharacters(in:with:)
.replaceCharacters(in:with:)
has the following declaration:The Playground code below shows how to use
replaceCharacters(in:with:)
in order to replace a substring of anNSMutableAttributedString
instance with a newNSMutableAttributedString
instance:#2. Using
NSMutableString
replaceOccurrences(of:with:options:range:)
methodNSMutableString
has a method calledreplaceOccurrences(of:with:options:range:)
.replaceOccurrences(of:with:options:range:)
has the following declaration:The Playground code below shows how to use
replaceOccurrences(of:with:options:range:)
in order to replace a substring of anNSMutableAttributedString
instance with a newNSMutableAttributedString
instance:我必须将
标记中的文本加粗,这是我所做的:
I had to bold text in
<b>
tags, here what I've done:我发现所有其他答案都不起作用。以下是我在类别扩展中替换 NSAttributed 字符串内容的方法:
I find that all of the other answers does not work. Here is how I replaced content of a NSAttributed string in a category extension:
我有一个特定的要求并固定如下。这可能会对某人有所帮助。
需求:在storyboard中,直接在UITextView的属性中添加富文本,其中包含“App Version: 1.0”一词。现在我必须通过从 info plist 读取版本号来动态化版本号。
解决方案:
从故事板中删除了版本号 1.0,只保留“应用程序版本:”并添加以下代码。
完毕!!更新/修改了属性文本。
I have a specific requirement and fixed like below. This might help someone.
Requirement: In the storyboard, rich text directly added to UITextView's attribute which contains a word "App Version: 1.0". Now I have to dynamise the version number by reading it from info plist.
Solution:
Deleted version number 1.0 from the storyboard, just kept "App Version:" and added below code.
Done!! Updated/modified attributedText.
我为该
用例创建了一个 Swift 5 扩展
i created a Swift 5 extension for that
use case
完整的解决方案
the full solution