Jacek Kwiecień
Jacek Kwiecień

Reputation: 12659

Calculating number of lines of dynamic UILabel (iOS7)

There are many solutions to this questions arround but couldn't find non-deprecated one.

I have an UILabel with mode WordWrap and fixed width of, let's say 250. Lines are set to 0.

Here is what I tried:

UILabel *contentLabel = (UILabel*)[contentView viewWithTag:10];
CGSize size = [contentLabel.text sizeWithFont:contentLabel.font forWidth:contentLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"Label's height is: %d", size.height);

The output of height param is always 20 (so it's like one line), while te text is like 30 lines long.

I need that for UIScrollView purposes.

Upvotes: 8

Views: 25944

Answers (7)

tausun
tausun

Reputation: 2160

This has worked for me:

*Set the numberOfLines property of UILabel to 0 *add this line: yourLabel.sizeToFit() after assigning text to the UILabel

Upvotes: 0

onkar dhanlobhe
onkar dhanlobhe

Reputation: 231

     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 = "Your 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. Happy coding!!!

Upvotes: 0

Sarat Patel
Sarat Patel

Reputation: 856

You can use this simple method:

which will return number of lines

- (int)lineCountForLabel:(UILabel *)label
{
    CGFloat labelWidth = label.frame.size.width;
    int lineCount = 0;
    CGSize textSize = CGSizeMake(labelWidth, MAXFLOAT);
    long rHeight = lroundf([label sizeThatFits:textSize].height);
    long charSize = lroundf(label.font.leading);
    lineCount = (int)( rHeight / charSize );
    return lineCount;
}

by calling

[self lineCountForLabel:YOUR_LABEL];

Upvotes: 1

AlexD
AlexD

Reputation: 833

I was having trouble using boundingRectWithSize directly on my UILabel's attributedText — it was not accounting for the wrap to multiple lines, (the returned height was always 17.5). To work around this, I had to use boundingRectWithSize on the UILabel's text property and pass in the attributes dictionary separately (and not via [self.myLabel.attributedText attributesAtIndex:0 effectiveRange:nil]).

CGRect labelSize = CGRectIntegral([self.myLabel.text     
    boundingRectWithSize:CGSizeMake(self.myLabel.frame.size.width, MAXFLOAT) 
    options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
    attributes:@{NSFontAttributeName:self.myLabel.font, 
    NSParagraphStyleAttributeName:paragraphStyle} context:nil]);

Upvotes: 4

Numeral
Numeral

Reputation: 1405

Use suggested in documentation method :

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);

E.g.

    CGSize maxSize = CGSizeMake(self.label.frame.size.width, MAXFLOAT);

    CGRect labelRect = [self.label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil];

    NSLog(@"size %@", NSStringFromCGSize(labelRect.size));

Upvotes: 55

Siddh
Siddh

Reputation: 712

Try this out

//Here tweetText is an object of NSString and assign a text to it  
NSString *tweetText = tweet.tweetText;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [tweetText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

//detailLabel is an object of UILabel    
[detailLabel setText:tweetText];

//Set frame
[detailLabel setFrame:CGRectMake(76,20,280, MAX(size.height, 30.0f))];

Upvotes: -3

ninja_iOS
ninja_iOS

Reputation: 1223

Here is method which I'm using:

CGSize maximumSize = CGSizeMake(contentLabel.frame.size.width, 9999);

CGSize myStringSize = [eventName sizeWithFont:contentLabel.font
                            constrainedToSize:maximumSize
                                lineBreakMode:UILineBreakModeWordWrap];

Upvotes: -6

Related Questions