cdub cdub - 1 year ago 98
Objective-C Question

How do I calculate the UILabel height dynamically

I have the following code:

label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = @"some long text";
[label sizeToFit];

How do I get the height of label in points?

Answer Source

Use following method to calculate dynamic UILabel height:

- (CGFloat)getLabelHeight:(UILabel*)label
    CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
    CGSize size;

    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [label.text boundingRectWithSize:constraint

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

    return size.height;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download