How Can I Underline the Button Text of UIButton?

后端 未结 4 1904
既然无缘
既然无缘 2021-02-03 14:09

The text is coming form a database. I would like to use it for a button and underline the text of the button. How can I do that?

4条回答
  •  孤城傲影
    2021-02-03 14:28

    In Swift 3 the following extension can be used for an underline:

    extension UIButton {
        func underlineButton(text: String) {
            let titleString = NSMutableAttributedString(string: text)
            titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
            self.setAttributedTitle(titleString, for: .normal)
        }
    } 
    

提交回复
热议问题