参考文章
- (NSInteger)needLinesWithWidth:(CGFloat)width;{
UILabel *label = [[UILabel alloc] init];
label.font = self.font;
NSString *text = self.text;
NSInteger sum = 0;
if (text.length != 0) {
NSArray *splitTexts = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *stext in splitTexts) {
label.text = stext;
CGSize textSize = [label systemLayoutSizeFittingSize:CGSizeZero];
NSInteger lines = ceilf(textSize.width / width);
lines = lines == 0 ? 1 : lines;
sum += lines;
}
}
return sum;
}
网友评论