UILabel 属性文本异常:在 Swift 中设置文本属性时出现意外的删除线

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

以下代码将创建红色文本,不带删除线。

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let text = "Hello World"
        let textCount = text.count
        let fullRange = NSRange(location: 0, length: textCount)
        
        var attributedText = NSMutableAttributedString(string: text)
        attributedText.addAttribute(.foregroundColor, value: UIColor.green, range: fullRange)
        attributedText.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: fullRange)
        label.attributedText = attributedText
        
        attributedText = NSMutableAttributedString(string: text)
        attributedText.addAttribute(.foregroundColor, value: UIColor.red, range: fullRange)
        attributedText.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: fullRange)
        label.attributedText = attributedText
    }
}

enter image description here


但是,如果我在中间触发

label.text
,则会导致以下奇怪的行为:红色文本,在函数末尾创建删除线

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let text = "Hello World"
        let textCount = text.count
        let fullRange = NSRange(location: 0, length: textCount)
        
        
        var attributedText = NSMutableAttributedString(string: text)
        attributedText.addAttribute(.foregroundColor, value: UIColor.green, range: fullRange)
        attributedText.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: fullRange)
        label.attributedText = attributedText
        
        // Why this will cause a red color text, with strike-through created at the end of function?
        label.text = text
        
        attributedText = NSMutableAttributedString(string: text)
        attributedText.addAttribute(.foregroundColor, value: UIColor.red, range: fullRange)
        attributedText.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: fullRange)
        label.attributedText = attributedText
    }
}

enter image description here

有谁知道这种行为背后的原因是什么,我该如何避免这种情况?谢谢你。

ios swift uikit uilabel
1个回答
0
投票

根据一些搜索和快速测试,这似乎是一个长期存在的问题。漏洞?怪癖?谁知道...

第一个注意事项——当你的代码执行此操作时:

    // 1
    attributedText = NSMutableAttributedString(string: text)
    // 2
    attributedText.addAttribute(.foregroundColor, value: UIColor.red, range: fullRange)
    // 3
    attributedText.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: fullRange)
    // 4
    label.attributedText = attributedText

// 3
行没有执行任何操作...您还没有添加删除线属性,因此无法删除它。

我见过的删除删除线的唯一方法是将其设置在新的属性字符串中,其值为

0
(零):

    attributedText = NSMutableAttributedString(string: text)
    attributedText.addAttribute(.foregroundColor, value: UIColor.red, range: fullRange)
    // set strikeThrough to Zero
    attributedText.addAttribute(.strikethroughStyle, value: 0, range: fullRange)
    label.attributedText = attributedText
© www.soinside.com 2019 - 2024. All rights reserved.