我的应用程序正在呈现一些分数结果,我想根据结果添加分数标签的颜色变化,从红色到绿色:
self.scoreLabel.text = "\(total100)/100"实现它的最简单方法是什么?
提前谢谢你
发布于 2020-04-17 17:01:23
当结果发生变化时调用此函数。
func setTextColor(score: Int){
if score < 34 {
self.scoreLabel.textColor = .red
}else {
self.scoreLabel.textColor = .green
}
}发布于 2020-04-17 15:50:47
您需要使用属性字符串
let myMutableString = NSMutableAttributedString(string: "\(total100)/100", attributes: [NSAttributedString.Key.font:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:7,length:3))
self.scoreLabel.attributedText = myMutableStringhttps://stackoverflow.com/questions/61266377
复制相似问题