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
Here you go, just tested it. (works in xCode 7 Beta at least)
@IBOutlet weak var yourButton: UIButton!
var attrs = [
NSFontAttributeName : UIFont.systemFontOfSize(19.0),
NSForegroundColorAttributeName : UIColor.redColor(),
NSUnderlineStyleAttributeName : 1]
var attributedString = NSMutableAttributedString(string:"")
override func viewDidLoad() {
super.viewDidLoad()
let buttonTitleStr = NSMutableAttributedString(string:"My Button", attributes:attrs)
attributedString.appendAttributedString(buttonTitleStr)
yourButton.setAttributedTitle(attributedString, forState: .Normal)
}
For swift 5
var attrs : [NSAttributedString.Key : Any] = [
NSAttributedString.Key.font : UIFont.systemFont(ofSize: 19.0),
NSAttributedString.Key.foregroundColor : UIColor.blue,
NSAttributedString.Key.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]
Here is done on the storyboard. (Xcode 9.1)
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()
}
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)
if you are looking for a way to do this without inheritance -
// in swift 4 - switch NSUnderlineStyleAttributeName with NSAttributedStringKey.underlineStyle
extension UIButton {
func underline() {
guard let text = self.titleLabel?.text else { return }
let attributedString = NSMutableAttributedString(string: text)
//NSAttributedStringKey.foregroundColor : UIColor.blue
attributedString.addAttribute(NSAttributedString.Key.underlineColor, value: self.titleColor(for: .normal)!, range: NSRange(location: 0, length: text.count))
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: self.titleColor(for: .normal)!, range: NSRange(location: 0, length: text.count))
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: text.count))
self.setAttributedTitle(attributedString, for: .normal)
}
}
extension UILabel {
func underline() {
if let textString = self.text {
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedString.length - 1))
attributedText = attributedString
}
}
}