Underline button text in Swift

后端 未结 14 1830
暖寄归人
暖寄归人 2021-01-30 10:26

I have UIButton. In interface builder I set its title to be \'Attributed\'. How can I make its title to be underlined from code in Swift?

@IBOutlet weak var myBt         


        
14条回答
  •  清酒与你
    2021-01-30 10:33

    • Swift 5.2.4
    • Xcode 11.5
    let attributes: [NSAttributedString.Key : Any] = [
    NSAttributedString.Key.underlineStyle: 1,
    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13),
    NSAttributedString.Key.foregroundColor: UIColor.systemGray3
    ]
    
    let attributedString = NSMutableAttributedString(string: "Text here", attributes: attributes)
    button.setAttributedTitle(NSAttributedString(attributedString: attributedString), for: .normal)
    

提交回复
热议问题