在给定字符串The * quick * brown * fox * jumps * over * the * lazy * dog
的情况下,我按如下方式应用字体修饰符,以使all occurrences of asterisk的样式与文本的其余部分不同.
func customDesc(_ text: String) -> NSMutableAttributedString {
let asteriskTextRange = (text as NSString).range(of: "*")
let attributedString = NSMutableAttributedString(string: text, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15, weight: .semibold)])
attributedString.setAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20, weight: .heavy)], range: asteriskTextRange)
return attributedString
}
看起来range(of:)
只会 Select 第一个匹配项,那么如何更新所有匹配项并在Text
视图中显示结果文本呢?我找不到任何接受NSMutableAttributedString
的初始值设定项.如有任何帮助,我们不胜感激.