如何快速%@改变动态字符串的颜色?

问题描述 投票:0回答:1

我在字符串中使用以下占位符:%@

例如,我的字符串是String(format: “ greetings%@”,str)

如何为该动态占位符提供特定的颜色?

ios swift nslocalizedstring
1个回答
0
投票
我们使用属性字符串,并且不会使用格式字符串来构建它,而只是附加str

let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.green] let name = NSAttributedString(string: str, attributes: attributes) let greeting = NSMutableAttributedString(string: "greetings ") string.append(name) label.attributedText = string

© www.soinside.com 2019 - 2024. All rights reserved.