The text is coming form a database. I would like to use it for a button and underline the text of the button. How can I do that?
Highlight the text in the Attributes inspector. Right click, choose Font and then Underline.
The property text-decoration-line is used to underline the text. This property has three values that are overline, underline, or line-through. So, the value underline is used to underline the text in CSS. This value draws the underline beneath the inline text.
The quickest way to underline text is to press Ctrl+U and start typing. When you want to stop underlining, press Ctrl+U again. You can also underline text and spaces in several other ways.
In iOS 6, NSAttributedString is used modifying the text, you can use "NSMutableAttributedString" for multi color text, font, style, etc using single UIButton or UILabel.
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"The Underlined text"];
// making text property to underline text-
[titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])];
// using text on button
[button setAttributedTitle: titleString forState:UIControlStateNormal];
For this you can subclass UILabel
and overwrite its -drawRect
method and then use your own UILabel
and add a UIButton
over it of custom type.
Make your drawRect method in UILabel as
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f);
CGContextSetLineWidth(context, 1.0f);
CGContextMoveToPoint(context, 0, self.bounds.size.height - 1);
CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height - 1);
CGContextStrokePath(context);
[super drawRect:rect];
}
In Swift 3 the following extension can be used for an underline:
extension UIButton {
func underlineButton(text: String) {
let titleString = NSMutableAttributedString(string: text)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
self.setAttributedTitle(titleString, for: .normal)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With