Dynamically getting height of UILabel according to

2019-01-08 13:57发布

I am using the this method for getting the height of the UILabel Dynamically:

+(CGSize) GetSizeOfLabelForGivenText:(UILabel*)label Font:(UIFont*)fontForLabel Size:  (CGSize)LabelSize{
    label.numberOfLines = 0;
    CGSize labelSize = [label.text sizeWithFont:fontForLabel constrainedToSize:LabelSize lineBreakMode:NSLineBreakByCharWrapping];
    return (labelSize);
}

With this solution I am getting the exact size of UILabel if my code is running on below iOS 8 but if I run my application on iOS7 then it is returns a different value.

13条回答
Bombasti
2楼-- · 2019-01-08 14:20

The accepted answer is too long. You can use the following:

+(CGSize) GetSizeOfLabelForGivenText:(UILabel*)label Font:(UIFont*)fontForLabel Size:  (CGSize) constraintSize{
    label.numberOfLines = 0;
    CGRect labelRect = [label.text boundingRectWithSize:constraintSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:fontForLabel} context:nil];
    return (labelRect.size);
}
查看更多
叛逆
3楼-- · 2019-01-08 14:22

if you are using any of the system fonts, they changed in iOS 7 so they would be different sizes.


Also, sizeWithFont:constrainedToSize:lineBreakMode: is deprecated in iOS 7. Use sizeWithAttributes: instead (if you are on iOS 7)

查看更多
做自己的国王
4楼-- · 2019-01-08 14:26

this code is for moving four labels clockwise with the button tap, by using function for button:

(void)onclick
{
    CGRect newFrame;
    CGRect newFrame1 = lbl1.frame;
    CGRect newFrame2 = lbl2.frame;
    CGRect newFrame3 = lbl3.frame;
    CGRect newFrame4 = lbl4.frame;
    lbl1.frame=newFrame;
    lbl4.frame=newFrame1;
    lbl3.frame=newFrame4;
    lbl2.frame=newFrame3;
    lbl1.frame=newFrame2;
}
查看更多
聊天终结者
5楼-- · 2019-01-08 14:28

Super simple. Just get the area of the text, divide by width, then round up to the nearest height that will fit your font.

+ (CGFloat)heightForText:(NSString*)text font:(UIFont*)font withinWidth:(CGFloat)width {
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName:font}];
    CGFloat area = size.height * size.width;
    CGFloat height = roundf(area / width);
    return ceilf(height / font.lineHeight) * font.lineHeight;
}

Very much a plug and play solution. I use it in a helper class a lot, especially for dynamically sized UITableViewCells.

Hope this helps others in the future!

查看更多
Viruses.
6楼-- · 2019-01-08 14:30

I have a situation where i need to set the height of label dynamically according to text. I am using Xcode 7.1 and my project deployment target is 7.0 but i tested it on iOS 9 simulator and following solution works for me. Here is the solution: First of of you will create a dictionary like this:

NSDictionary *attributes = @{NSFontAttributeName:self.YOUR_LABEL.font};

now we will calculate the height and width for our text and pass the newly created dictionary.

    CGRect rect = [YOUR_TEXT_STRING boundingRectWithSize:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:attributes
                                          context:nil];

Then we will set the frame of our LABEL:

    self.YOUR_LABEL.frame = CGRectMake(self.YOUR_LABEL.frame.origin.x, self.YOUR_LABEL.frame.origin.y, self.YOUR_LABEL.frame.size.width, rect.size.height);

THIS IS HOW I SUCCESSFULLY SET THE FRAME OF MY LABEL ACCORDING TO TEXT.

查看更多
虎瘦雄心在
7楼-- · 2019-01-08 14:31

Here's a total solution for width and height. Put these in your AppDelegate:

+(void)fixHeightOfThisLabel:(UILabel *)aLabel
{
    aLabel.frame = CGRectMake(aLabel.frame.origin.x,
                              aLabel.frame.origin.y,
                              aLabel.frame.size.width,
                              [AppDelegate heightOfTextForString:aLabel.text
                                                         andFont:aLabel.font
                                                         maxSize:CGSizeMake(aLabel.frame.size.width, MAXFLOAT)]);
}

+(CGFloat)heightOfTextForString:(NSString *)aString andFont:(UIFont *)aFont maxSize:(CGSize)aSize
{
    // iOS7
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        CGSize sizeOfText = [aString boundingRectWithSize: aSize
                                                  options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                               attributes: [NSDictionary dictionaryWithObject:aFont
                                                                                       forKey:NSFontAttributeName]
                                                  context: nil].size;

        return ceilf(sizeOfText.height);
    }

    // iOS6
    CGSize textSize = [aString sizeWithFont:aFont
                          constrainedToSize:aSize
                              lineBreakMode:NSLineBreakByWordWrapping];
    return ceilf(textSize.height;
}

+(void)fixWidthOfThisLabel:(UILabel *)aLabel
{
    aLabel.frame = CGRectMake(aLabel.frame.origin.x,
                              aLabel.frame.origin.y,
                                [AppDelegate widthOfTextForString:aLabel.text
                                                          andFont:aLabel.font
                                                          maxSize:CGSizeMake(MAXFLOAT, aLabel.frame.size.height)],
                              aLabel.frame.size.height);
}

+(CGFloat)widthOfTextForString:(NSString *)aString andFont:(UIFont *)aFont maxSize:(CGSize)aSize
{
    // iOS7
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        CGSize sizeOfText = [aString boundingRectWithSize: aSize
                                                  options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                               attributes: [NSDictionary dictionaryWithObject:aFont
                                                                                       forKey:NSFontAttributeName]
                                                  context: nil].size;

        return ceilf(sizeOfText.width);
    }

    // iOS6
    CGSize textSize = [aString sizeWithFont:aFont
                          constrainedToSize:aSize
                              lineBreakMode:NSLineBreakByWordWrapping];
    return ceilf(textSize.width);
}

then to use this, set the label's text:

label.numberOfLines = 0;
label.text = @"Everyone loves Stack OverFlow";

and call:

[AppDelegate fixHeightOfThisLabel:label];

Note: label's numberOfLines has to be set to 0. Hope that helps.

查看更多
登录 后发表回答