Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underline button text in Swift

Tags:

xcode

ios

swift

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 myBtn: UIButton! 

I created a function called on the touchUpInside event of this button:

var attributedString = NSMutableAttributedString(string:"new text")     var attrs = [         NSFontAttributeName : UIFont.systemFontOfSize(19.0),         NSForegroundColorAttributeName : UIColor.redColor()     ]     var gString = NSMutableAttributedString(string:"g", attributes:attrs)     attributedString.appendAttributedString(gString)      myBtn.titleLabel?.attributedText = attributedString; 

But still no result. Also I need to know how to access the underline attribute. Text, size and color stay the same.

like image 907
moonvader Avatar asked Jul 11 '15 12:07

moonvader


People also ask

How do I underline a label in Xcode?

Select the text of the label, right click and change the font to 'underline'.

How do you underline text in Swift?

To make the text on UILabel underlined we will need to use the NSMutableAttributedString together with NSUnderlineStyleAttributeName. The Swift code snippet below demonstrates how to create a new UILabel programmatically and then use the NSMutableAttributedString to underline the text on the label.


1 Answers

Swift 5 / Xcode 12/13

  @IBOutlet weak var myButton: UIButton!       let yourAttributes: [NSAttributedString.Key: Any] = [       .font: UIFont.systemFont(ofSize: 14),       .foregroundColor: UIColor.blue,       .underlineStyle: NSUnderlineStyle.single.rawValue   ] // .double.rawValue, .thick.rawValue                override func viewDidLoad() {      super.viewDidLoad()           let attributeString = NSMutableAttributedString(         string: "Your button text",         attributes: yourAttributes      )      myButton.setAttributedTitle(attributeString, for: .normal)   } 

Swift 4 / Xcode 9

  @IBOutlet weak var myButton: UIButton!       let yourAttributes: [NSAttributedStringKey: Any] = [       NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14),       NSAttributedStringKey.foregroundColor: UIColor.blue,       NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue   ] // .styleDouble.rawValue, .styleThick.rawValue, .styleNone.rawValue                   override func viewDidLoad() {     super.viewDidLoad()          let attributeString = NSMutableAttributedString(       string: "Your button text",       attributes: yourAttributes     )     myButton.setAttributedTitle(attributeString, for: .normal)   } 

Swift 3 / Xcode 8

  @IBOutlet weak var myButton: UIButton!    let yourAttributes: [String: Any] = [       NSFontAttributeName: UIFont.systemFont(ofSize: 14),       NSForegroundColorAttributeName: UIColor.white,       NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue   ] // .styleDouble.rawValue, .styleThick.rawValue, .styleNone.rawValue                  override func viewDidLoad() {       super.viewDidLoad()        let attributeString = NSMutableAttributedString(         string: "Your button text",         attributes: yourAttributes       )               myButton.setAttributedTitle(attributeString, for: .normal)      } 

enter image description here

like image 87
A.Kant Avatar answered Oct 06 '22 00:10

A.Kant