我有3个按钮,我希望当前选中的按钮有一个彩色下划线。当我为所选状态设置UIButton的属性标题时,它会在选中时正确显示,但是当它被取消选中时,它仍然带有下划线。然后,我尝试为.selected
和.normal
设置属性字符串,一个带有下划线,另一个没有,然后所选按钮不显示/闪烁一秒然后消失。
let selectedAtts: [String : Any] = [
NSForegroundColorAttributeName: UIColor.black,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
NSUnderlineColorAttributeName: UIColor.magenta]
let normalAtts: [String : Any] = [
NSForegroundColorAttributeName: UIColor.black,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue]
for button in buttonList {
let selectedText = NSMutableAttributedString(string: (button.titleLabel?.text)!)
selectedText.addAttributes(selectedAtts, range: NSRange(location: 0, length: selectedText.length))
let normalText = NSMutableAttributedString(string: (button.titleLabel?.text)!)
normalText.addAttributes(normalAtts, range: NSRange(location: 0, length: normalText.length))
button.setAttributedTitle(normalText, for: .normal)
button.setAttributedTitle(selectedText, for: .selected)
}
有没有人遇到过这个并理解为什么会这样?