今天使用Label的时候,需要计算UILabel的高度,发现在4,4s上显示正常,在iPhone5s、iphone6上显示出错
使用的方法为:
NSString *teacherComment = @"评语是写给学生看的,所以一方面评语要使用学生能看得懂的英语来写,所使用的词汇和语法不能过高或过低于学生的现有水平,要切合学生的实际水平,符合学生的个性心理...";
float commentHeight = 0.0;
if ([teacherComment isEqual:@""]||teacherComment==nil||[[NSNull null] isEqual:teacherComment]) {
commentHeight = BASIC_LABEL_HEIGHT;
}else{
CGSize commentRect = [teacherComment sizeWithFont:[UIFont fontWithName:TITLE_FONT size:FONT_SIZE_FOURTEEN] constrainedToSize:CGSizeMake(180, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping];
if (commentRect.height > BASIC_LABEL_HEIGHT) {
commentHeight = commentRect.height;
}
else
{
commentHeight = BASIC_LABEL_HEIGHT;
}
}这个问题很奇怪了,在此记录,上网搜索一下,使用一下方法即可:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:TITLE_FONT size:FONT_SIZE_FOURTEEN], NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize labelSize = [teacherCommentLabel.text boundingRectWithSize:CGSizeMake(180.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
labelSize.height = ceil(labelSize.height);
labelSize.width = ceil(labelSize.width);
if (labelSize.height > BASIC_LABEL_HEIGHT) {
commentHeight = labelSize.height;
}
else
{
commentHeight = BASIC_LABEL_HEIGHT;
}误差如图所示:
如果您有什么好的方法,请留言告知,谢谢。。。
延伸阅读:www.wahenzan.com
ios 计算label宽度(高度)出错在iPhone5s、iphone6上出错
原文地址:http://blog.csdn.net/gloryflow/article/details/42008877