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

Sara Canducci
  • 6,231
  • 5
  • 19
  • 24
  • 1
    Why don't you use 0 for the number of lines? – Larme Sep 01 '14 at 11:12
  • Maybe this [tutorial](http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout) 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 layout – Antonio Sep 01 '14 at 11:15
  • may be it's helpful for you:- http://stackoverflow.com/questions/9181368/ios-dynamic-sizing-labels – Renish Dadhaniya Sep 01 '14 at 11:23

5 Answers5

12
myUILabel.numberOfLines = 0;
myUILabel.text = @"Pass random text here";
[myUILabel sizeToFit];
codeIgnitor
  • 765
  • 1
  • 5
  • 16
  • 1
    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.height – Goppinath Sep 01 '14 at 11:48
  • 1
    and you have to mention that the statement [myUILabel sizeToFit]; must be the last. – Goppinath Sep 01 '14 at 12:02
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?

derdida
  • 14,784
  • 16
  • 90
  • 139
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...

Goppinath
  • 10,569
  • 4
  • 22
  • 45
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
}
Oscar Falmer
  • 1,771
  • 1
  • 24
  • 38
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!!!

onkar dhanlobhe
  • 231
  • 2
  • 13