今天使用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;
}
誤差如圖所示:
