Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get exactly height of UILabel with line space?

I'm writing a expandable tableView header, I need to set the height of header accurately in tableView delegate method. I'm now using the following method to calculate the height of my header(a multi-line label):

CGRect calcuRect = [headerText boundingRectWithSize:CGSizeMake(myLabelWitdh, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:myLabelFont} context:nil];
CGFloat headerHeight = calcuRect.size.height;

However, I've found that the height calculated out is just about the text without include line space. So how can I get the line space height of the label? or how to get accurate height of UILabel with line space?

like image 655
ZyusAn Avatar asked Apr 09 '16 04:04

ZyusAn


2 Answers

You can get the exact height of UILabel with exactly passing the same font size and type and doing some calculations.
Here I used a UILabel with Helvetica font with font size 16.

Objective C

- (CGFloat)requiredHeight:(NSString*)labelText{

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:16.0];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, CGFLOAT_MAX)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = font;
    label.text = labelText;
    [label sizeToFit];
    return label.frame.size.height;

}

Output

CGFloat size = [self requiredHeight:@"iOS Rocks"];
NSLog(@"%f",size);

size = [self requiredHeight:@"iOS Rocks\n"];
NSLog(@"%f",size);

Console Output

2016-04-10 01:37:46.812 testPro[6093:327503] 18.500000
2016-04-10 01:37:46.814 testPro[6093:327503] 37.000000

Swift 2.2

func requiredHeight(labelText:String) -> CGFloat {

    let font = UIFont(name: "Helvetica", size: 16.0)
    let label:UILabel = UILabel(frame: CGRectMake(0, 0, 200, CGFloat.max))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.font = font
    label.text = labelText
    label.sizeToFit()
    return label.frame.height

}

Edit
Swift 3.0

func requiredHeight(labelText:String) -> CGFloat {

    let font = UIFont(name: "Helvetica", size: 16.0)
    let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: .max))
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    label.font = font
    label.text = labelText
    label.sizeToFit()
    return label.frame.height

}
like image 151
Rajan Maheshwari Avatar answered Sep 22 '22 10:09

Rajan Maheshwari


Based on Rajan Maheshwari answer I wrote this function in Swift 3.0 for a generic label with the optional line height. Hope it helps.

func heightForLabel(_ text: String, font: UIFont, width: CGFloat, lineSpacing: CGFloat) -> CGFloat {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.font = font
    if lineSpacing > 0.0 {
        let style = NSMutableParagraphStyle()
        style.lineSpacing = lineSpacing
        style.alignment = .center
        label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: style])
    } else {
        label.text = text
    }
    label.sizeToFit()
    return label.frame.height
}
like image 43
agfa555 Avatar answered Sep 20 '22 10:09

agfa555