How to find actual number of lines of UILabel?

前端 未结 14 2383
灰色年华
灰色年华 2020-11-27 13:10

How can I find the actual number of lines of a UILabel after I have initialized it with a text and a font? I have set

14条回答
  •  执念已碎
    2020-11-27 13:51

    You can find the total number of line available in your custom label Please check this code...

    NSInteger numberOfLines = [self lineCountForText:@"YOUR TEXT"];
    
    - (int)lineCountForText:(NSString *) text
    {
        UIFont *font = [UIFont systemFontOfSize: 15.0];
        int width=Your_LabelWidht;
    
        CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:@{NSFontAttributeName : font} context:nil];
        return ceil(rect.size.height / font.lineHeight);
    }
    

提交回复
热议问题