How to make an underlined text in UILabel?
Asked Answered
D

7

29

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?

Dichloride answered 29/10, 2012 at 13:1 Comment(7)
You have to use CoreTextScrobiculate
Can you please explain..Dichloride
stackoverflow.com/questions/2763757/core-text-tutorial see this answer it will helps youScrobiculate
I want to make the line with 0.5 thickness. Is it possible?Dichloride
I dont know much idea about coretext but i think it is possible with Core textScrobiculate
Swift extensionFreeborn
Refer this link https://mcmap.net/q/276897/-can-i-underline-the-text-of-a-nstextfield-from-interface-builderPasquinade
A
84

Objective-C

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]}];

Swift

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];

iOS 5.1.1 < version

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

Asmodeus answered 29/10, 2012 at 13:22 Comment(3)
But I need this underline in ios 4.3 also... :(Dichloride
i want to remove underline if set...how can i do thisDecode
Here's small optimization: With your swift version line let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Hello Good Morning"), the data type of attributeString is implied by what it is being assigned to, so it's redundant and you can omit it, as follows: let attributeString = NSMutableAttributedString(string: "Hello Good Morning")Maintain
K
14

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: enter image description here

enter image description here

enter image description here

enter image description here

We can also use for button too.

Knipe answered 30/9, 2017 at 5:39 Comment(0)
H
5

The Designer Way - Highly Customisable Version

I personally prefer to customise my design(s) as much as possible. Sometimes, you will find that using the built-in underline tool is not easily customisable.

Here is how I do it:

1 First I create a UIView for the underline. You can then choose your background color, either in IB or programmatically. Constraints are key here.

Create Your Underline view

2 Then, you can customise the underline as much as you like. For example, to adapt the width of the underline for better visual effect, create an outlet connection to a constraint in Interface Builder

enter image description here

3 You can then easily programmatically customise your underline view. For example here, to give you an idea we will make the underline 20 px wider than the title "Breads":

var originalString: String = "Breads"
let myString: NSString = originalString as NSString
let size: CGSize = myString.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)])
self.widthUnderlineTitle.constant = size.width + 20

Results:

enter image description here

Hijacker answered 4/12, 2016 at 3:19 Comment(0)
P
3

Take a look at TTTAttributedLabel. If you're allowed to use 3d-party components just replace your UILabels with TTTAttributedLabel where you need it (drop-in replacement). Works with iOS < 6.0!

Pismire answered 29/10, 2012 at 13:23 Comment(0)
F
2

Swift extension on UILabel:

Still needs improvement, any thoughts are welcome:

extension UILabel{

    func underLine(){    
        if let textUnwrapped = self.text{
            let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
            let underlineAttributedString = NSAttributedString(string: textUnwrapped, attributes: underlineAttribute)
            self.attributedText = underlineAttributedString   
        }   
    }
}

One of the cons i can think of right now is that it will need to be called each time text changes, and there is no actual way of turning it off...

Freeborn answered 8/4, 2016 at 18:46 Comment(0)
C
0

Swift 4.0

var attributeString = NSMutableAttributedString(string: "Hello Good Morning")
attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSRange)
Callable answered 10/11, 2017 at 10:55 Comment(0)
I
-1

Swift 5

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: yourLabel.text ?? "")
attributeString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSMakeRange(0, attributeString.length))
yourLabel.attributedText = attributeString
Inrush answered 10/12, 2019 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.