How to find UILabel's number of Lines

后端 未结 7 727
悲哀的现实
悲哀的现实 2020-11-28 05:35

I displayed the text in UILabel by using wrap method. Now I want to need to find the how many number of line is there in UILabel.

If there is any possible way to fi

7条回答
  •  再見小時候
    2020-11-28 06:15

    The method -sizeWithFont:constrainedToSize:lineBreakMode is now deprecated. You will want to use the method -boundingRectWithSize:options:attributes:context: now.

    Here's an examples:

    CGSize boundingRectSize = CGSizeMake(widthToConstrainTo, CGFLOAT_MAX);
    NSDictionary *attributes = @{NSFontAttributeName : [UIFont fontWithName:fontName size:14]};
    CGRect labelSize = [labelString boundingRectWithSize:boundingRectSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                    attributes:attributes
                                                       context:nil];
    

    In the above example I know the width I want to constrain the label to but since I'm not sure of the height, I just max the height param out using CGFLOAT_MAX. For the options you need to use NSStringDrawingUsesLineFragmentOrigin and NSStringDrawingUsesFontLeading if you're trying to calculate the size a label that can be any number of lines.

提交回复
热议问题