How to find UILabel's number of Lines

后端 未结 7 749
悲哀的现实
悲哀的现实 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:19

    sizeWithFont is deprecated in iOS 7, you may use this instead:

    - (int)lineCountForText:(NSString *) text
    {
        UIFont *font = ...
    
        CGRect rect = [text boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName : font}
                                         context:nil];
    
        return ceil(rect.size.height / font.lineHeight);
    }
    

提交回复
热议问题