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
Based on some of previous answers I decide to make a class that can be easy implemented into your apps
Swift 4
import UIKit
class UnderlineTextButton: UIButton {
override func setTitle(_ title: String?, for state: UIControlState) {
super.setTitle(title, for: .normal)
self.setAttributedTitle(self.attributedString(), for: .normal)
}
private func attributedString() -> NSAttributedString? {
let attributes : [NSAttributedStringKey : Any] = [
NSAttributedStringKey.font : UIFont.systemFont(ofSize: 19.0),
NSAttributedStringKey.foregroundColor : UIColor.red,
NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]
let attributedString = NSAttributedString(string: self.currentTitle!, attributes: attributes)
return attributedString
}
}
From code I call it on such a way
button.setTitle(author, for: .normal)