ios – 在NSAttributedString中替换整个文本字符串,而不修改其他属性

前端之家收集整理的这篇文章主要介绍了ios – 在NSAttributedString中替换整个文本字符串,而不修改其他属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个NSAttributedString的引用,我想改变属性字符串的文本.

我想我必须创建一个新的NSAttributedString并使用这个新的字符串更新引用.然而,当我这样做,我失去了以前的字符串的归因.

NSAttributedString *newString = [[NSAttributedString alloc] initWithString:text];
[self setAttributedText:newString];

我引用了self.attributedText中的旧的归属字符串.我如何保留以前归属于新字符串?

解决方法

您可以使用NSMutableAttributedString,只是更新字符串,属性将不会更改.
例:
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:@"my string" attributes:@{NSForegroundColorAttributeName: [UIColor blueColor],NSFontAttributeName: [UIFont systemFontOfSize:20]}];

//update the string
[mutableAttributedString.mutableString setString:@"my new string"];
原文链接:https://www.f2er.com/iOS/336405.html

猜你在找的iOS相关文章