Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Underlined Text in a Button on iOS?

I want to display the mail ID in my view such that clicking the mail ID should open the mail composer view.

I want to display the button text as underlined to show it is a hyperlink and for the button click event to call the mail composer view. At present, I am not able to show the button text underlined.

I thought of placing a label above the button and adjusting the label property to get the underlined text but that did not work.

Is there a different way to get the appearance and behavior I want?

like image 433
Warrior Avatar asked Apr 15 '10 12:04

Warrior


People also ask

What is the underline button?

Underline words and the spaces between them The quickest way to underline text is to press Ctrl+U and start typing.


2 Answers

Adding a screenshot for @Haider's answer.

Note: Only the text you highlight gets underlined, so you can underline just a specific range if you want.

enter image description here

I ended up underlining it in code anyway, just because I couldn't get Bold to work in conjunction with Underline for some reason. Could have been because of the font I was using, who knows.

@IBOutlet weak var underlineButton: UIButton! {
    didSet {
        let attrs = [
            NSFontAttributeName : UIFont(name: "MyCustomFont-Bold", size: 19.0)!,
            NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
            NSForegroundColorAttributeName : UIColor.orangeColor()
        ]
        let attrString = NSMutableAttributedString(string: "Button text", attributes:attrs)
        underlineButton.setAttributedTitle(attrString, forState: .Normal)
    }
}
like image 136
Robert Chen Avatar answered Sep 30 '22 21:09

Robert Chen


To do it in interface builder

  1. Click on button/label
  2. In Attributes inspector, change Title/Text to Attributed from plain
  3. Select the text which you want to show.
  4. Right click on it Goto Font->Underline
like image 31
Haider Avatar answered Sep 30 '22 21:09

Haider