Set UILabel's number of lines depending by the text contained - Objective C and/or Swift
Asked Answered
P

5

12

I'm trying to display some random text into a UILabel and of course I have nothing but its width. Is there a way to set my UILabel's height and/or number of lines depending by the text contained in it?

Thanks ;)

Poteen answered 1/9, 2014 at 11:7 Comment(3)
Why don't you use 0 for the number of lines?Shaikh
Maybe this tutorial can help you - it's about table cells with dynamic height, but I think you can transpose the concept into whatever you are doing. It's helpful if you are dealing with auto layoutAduwa
may be it's helpful for you:- https://mcmap.net/q/456155/-ios-dynamic-sizing-labelsLeftwards
T
12
myUILabel.numberOfLines = 0;
myUILabel.text = @"Pass random text here";
[myUILabel sizeToFit];
Traveller answered 1/9, 2014 at 11:14 Comment(2)
Thanks for the info. Its really easy thats why I have up voted. Please write more descriptive and the concrete answer for the question which is the heigh. myUILabel.frame.size.heightCritchfield
and you have to mention that the statement [myUILabel sizeToFit]; must be the last.Critchfield
B
4

Or (if you need the height of the label for a specific width and font size, you could calculate it with this):

func getStringHeight(mytext: String, fontSize: CGFloat, width: CGFloat)->CGFloat {

    let font = UIFont.systemFontOfSize(fontSize)
    let size = CGSizeMake(width,CGFloat.max)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .ByWordWrapping;
    let attributes = [NSFontAttributeName:font,
        NSParagraphStyleAttributeName:paragraphStyle.copy()]

    let text = mytext as NSString
    let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, context:nil)
    return rect.size.height  
}

You need this for Swift or for Objective C?

Burger answered 1/9, 2014 at 11:29 Comment(0)
C
1

You can use NSString UIKIT Addition method

- (CGSize)sizeWithFont:(UIFont *)font
     constrainedToSize:(CGSize)size
         lineBreakMode:(NSLineBreakMode)lineBreakMode

to calculate the hight. something like.

CGSize yourLabelSize = [@"Your very long text" sizeWithFont:yourFont constrainedToSize:CGSizeMake(desiredMaximumWidth, 2000) lineBreakMode:NSLineBreakByWordWrapping];

its really important to understand the constrainedToSize parameter. You have to pass a CGSize with desired maximum width and maximum possible height. Use the same UIFont with your label. Dont forget to set the

[yourLabel setNumberOfLines:0];

But the method is already deprecated in iOS 7 therefore you have to use

- (CGRect)boundingRectWithSize:(CGSize)size
                       options:(NSStringDrawingOptions)options
                    attributes:(NSDictionary *)attributes
                       context:(NSStringDrawingContext *)context

yourLabelSize.height will give you the height

hope it will help you...

Critchfield answered 1/9, 2014 at 11:33 Comment(0)
L
1

For Swift 4.0

func getStringHeight(mytext: String, fontSize: CGFloat, width: CGFloat)->CGFloat {

    let font = UIFont.systemFont(ofSize: fontSize)
    let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byWordWrapping;
    let attributes = [NSAttributedStringKey.font:font,
                      NSAttributedStringKey.paragraphStyle:paragraphStyle.copy()]

    let text = mytext as NSString
    let rect = text.boundingRect(with: size,
                                options:.usesLineFragmentOrigin,
                                attributes: attributes,
                                context:nil)
    return rect.size.height
}
Lith answered 17/11, 2017 at 21:22 Comment(0)
P
0
let l = UILabel()
l.numberOfLines = 0
l.layer.frame.size.width = self.view.frame.width - 40 /*padding(20 + 20)*/

l.font = UIFont(name: "BwModelica-Bold", size: 16.0)

l.text = "Random Any length Text!!"

let noOfLines = ceil(l.intrinsicContentSize.width) / l.frame.width)

let lbl_height = noOfLines * l.intrinsicContentSize.height

This will be your Exact dynamic height of Label and Number of lines. Happy coding!!!

Property answered 18/1, 2019 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.