Underline button text in Swift

后端 未结 14 1858
暖寄归人
暖寄归人 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:50

    Thanks for posting your code, it wasn't clear that you knew how to create an attributed string at all.

    This should work:

    var attrs = [
        NSFontAttributeName : UIFont.systemFontOfSize(19.0),
        NSForegroundColorAttributeName : UIColor.redColor(),
        NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleSingle.rawValue
    ]
    

    Swift 4 version:

    var attrs : [NSAttributedStringKey : Any] = [
        NSAttributedStringKey.font : UIFont.systemFont(ofSize: 19.0),
        NSAttributedStringKey.foregroundColor : UIColor.red,
        NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
    ]
    

提交回复
热议问题