How can I make an underlined text in UILabel
?
I had tried by making a UILabel
with height 0.5f and place it under the text. This line label is not visible when I am running my app in iPhone 4.3 and iPhone 4.3 simulator, but it is visible in iPhone 5.0 simulator onwards. Why?
How can I do this?
If you already have the extension for the UILabel, IMO it is simpler to call myButton. titleLabel?. underline(), or at least use it inside the underline() function in the extension for UIButton.
Select the text of the label, right click and change the font to 'underline'.
Swift5 UILabel Extension. Use this code in that case. Show activity on this post. Change the text property to attributed and select the text and right click to get the font property. Click on the strikethrough.
iOS 6.0 > version
UILabel
supports NSAttributedString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello Good Morning"]; [attributeString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:(NSRange){0,[attributeString length]}];
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Hello Good Morning") attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))
Definition :
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
Parameters List:
name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.
value : The attribute value associated with name.
aRange : The range of characters to which the specified attribute/value pair applies.
Now use like this:
yourLabel.attributedText = [attributeString copy];
You needs 3 party attributed Label
to display attributed text
:
1) Refer TTTAttributedLabel link. Its best third party attributed
Label
to display
attributed text
.
2) refer OHAttributedLabel for third party attributed
Label
I am using Xcode 9 and iOS 11. To make the UILabel with an underline beneath it. You can use both 1. Using code 2. Using xib
Using code:
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"I am iOS Developer"]; [attributeString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:(NSRange){0,[attributeString length]}]; lblAttributed.attributedText = attributeString;
Using Xib:
We can also use for button too.
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