#ios #swift #xcode #swift4
#iOS #swift #xcode #swift4
Вопрос:
// ВОТ КОД, в КОТОРОМ я ТАКЖЕ ИСПОЛЬЗОВАЛ ЦВЕТНУЮ МЕТКУ В СТРОКЕ
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.
}
Ответ №1:
У вас есть 2 варианта
1 замените значение на replacingOccurrences, но необходимо учитывать, сколько цифр может содержать число. 2 воссоздайте всю строку заново, что быстрее и меньше работы
Ответ №2:
Вы можете использовать didSet внутри cellIndex.
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, оно обновит метку нужной строкой.
Комментарии:
1. Не могли бы вы, пожалуйста, рассказать мне, каково точное функционирование didset?
2. Оно вызывается после присвоения переменной значения.
3. Хорошее решение .. (y)