根据字体大小,行间距计算UILabel行数
先根据要求计算文字尺寸:
-(CGSize)boundingRectWithWidth:(CGFloat)maxWidth
withTextFont:(UIFont *)font
withLineSpacing:(CGFloat)lineSpacing
text:(NSString *)text{
CGSize maxSize = CGSizeMake(maxWidth, CGFLOAT_MAX);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
//计算文字尺寸
CGSize size = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
return size;
}
UIFont* font = [UIFont systemFontOfSize:12];
CGFloat lineSpace = 6;//行间距
CGFloat textheight = [self boundingRectWithWidth:self.updateDesc.frame.size.width withTextFont:font withLineSpacing:lineSpace text:self.updateitem.desc].height;//实际文字高度
float space = font.lineHeight*2 + lineSpace;//字高、行高(此处限定为两行字+一个行高,即两行的高度)
可根据if (textheight > space) 比较,做出相关操作。