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
This is my solution. And to be honest you probably need this more than one place, so let's create an extension. This is swift 5.0 Cheers :)
extension UIButton {
func underline() {
guard let title = self.titleLabel else { return }
guard let tittleText = title.text else { return }
let attributedString = NSMutableAttributedString(string: (tittleText))
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: (tittleText.count)))
self.setAttributedTitle(attributedString, for: .normal)
}
}
And you can use it like this.
override func viewDidLoad() {
super.viewDidLoad()
button.underline()
}