//这里是我在字符串中也使用了彩色标签的代码
var cellIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
cellIndex = 1
let s1 = "word \(cellIndex) to \(cellIndex+7)"
let mystring:NSString = "\(s1) recovery phrases" as NSString
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string:mystring as String, attributes: [NSAttributedString.Key.font:Font.mediumRegularFont()])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: Color.darkColor(), range: NSRange(location:0,length:s1.count))
stepBarView.attributedText = myMutableString
}
@IBAction func onNextButtonTap(_ sender: Any) {
// i want to change cellIndex Value every time when i press button.
}
答案 0 :(得分:0)
您有2个选项
1用 replacingOccurrences 替换该值,但需要考虑该数字可能有多少个数字。 2再次重新创建整个字符串,速度更快,工作量更少
答案 1 :(得分:0)
您可以在cellIndex内使用didSet。
var cellIndex: Int = 0 {
didSet {
let s1 = "word \(cellIndex) to \(cellIndex+7)"
let mystring:NSString = "\(s1) recovery phrases" as NSString
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string:mystring as String, attributes: [NSAttributedString.Key.font:Font.mediumRegularFont()])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: Color.darkColor(), range: NSRange(location:0,length:s1.count))
stepBarView.attributedText = myMutableString
}
}
现在,每当将值设置为cellIndex时,它将使用所需的字符串更新标签。