如何在归因后重置或反转字符串
How to reset or reverse a String after its been attributed
我有一个 TableView
,在每个单元格中,我有一个 UISwitch
,如果启用,会修改行中的标签。
以下是我的修改方式:
@IBAction func completedTask(_ sender: UISwitch) {
//Getting original taskLabel
let initalLabel = taskLabel.text
//Modifying the string to have a line through it. Storing it in variable attributeString
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: taskLabel.text!)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
if sender.isOn{
print("attributed Label --> ",attributeString)
taskLabel.textColor = UIColor.red
taskLabel.attributedText = attributeString
}else{
print("initial Label --> ",initalLabel!)
taskLabel.text = initalLabel
taskLabel.textColor = UIColor.black
}
}
我 运行 遇到了将标签重置为原始字符串的问题。我现在就做一个演示。我添加了几个打印语句来帮助调试。我们可以看到 initialLabel 拥有正确的单元格值,但由于某种原因没有分配它。
这是演示:
为什么我的 taskLabel 显示的字符串不正确?
您需要删除关闭状态下的删除线
Removes the named attribute from the characters in the specified range. Ref removeAttribute:range:
@IBAction func completedTask(_ sender: UISwitch) {
//Modifying the string to have a line through it. Storing it in variable attributeString
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: taskLabel.text!)
if sender.isOn{
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
}else{
attributeString.removeAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
}
taskLabel.textColor = sender.isOn ? .red : .black
taskLabel.attributedText = attributeString
}
我有一个 TableView
,在每个单元格中,我有一个 UISwitch
,如果启用,会修改行中的标签。
以下是我的修改方式:
@IBAction func completedTask(_ sender: UISwitch) {
//Getting original taskLabel
let initalLabel = taskLabel.text
//Modifying the string to have a line through it. Storing it in variable attributeString
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: taskLabel.text!)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
if sender.isOn{
print("attributed Label --> ",attributeString)
taskLabel.textColor = UIColor.red
taskLabel.attributedText = attributeString
}else{
print("initial Label --> ",initalLabel!)
taskLabel.text = initalLabel
taskLabel.textColor = UIColor.black
}
}
我 运行 遇到了将标签重置为原始字符串的问题。我现在就做一个演示。我添加了几个打印语句来帮助调试。我们可以看到 initialLabel 拥有正确的单元格值,但由于某种原因没有分配它。
这是演示:
为什么我的 taskLabel 显示的字符串不正确?
您需要删除关闭状态下的删除线
Removes the named attribute from the characters in the specified range. Ref removeAttribute:range:
@IBAction func completedTask(_ sender: UISwitch) {
//Modifying the string to have a line through it. Storing it in variable attributeString
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: taskLabel.text!)
if sender.isOn{
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
}else{
attributeString.removeAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
}
taskLabel.textColor = sender.isOn ? .red : .black
taskLabel.attributedText = attributeString
}