iOS踩过的坑系列

iOS踩过的坑之根据文字内容计算标签高度

2019-04-24  本文已影响0人  Shaw1211

重要的事情说三遍!!!

一定要保证计算时的字体和宽度与显示时的字体和宽度一致!!!

一定要保证计算时的字体和宽度与显示时的字体和宽度一致!!!

一定要保证计算时的字体和宽度与显示时的字体和宽度一致!!!

 /** 计算笔记高度开始*/
UIFont *noteFont = [UIFont fontWithName:kFontName_Regular size:15 * rw];
//笔记的行间距
CGFloat noteSpace = 0;
        
CGFloat noteWidth = kScreenWidth - 2 * (13 + 5) * rw;
CGFloat noteHeight = [model.noteContent boundingRectWithSize:CGSizeMake(noteWidth, MAXFLOAT)
 font:noteFont lineSpacing:noteSpace].height;
model.noteHeight = noteHeight;
NSUInteger limitNoteCount = 6;
//文字高度超过N行,截取N行的高度,否则有多少显示多少
CGFloat limitNoteHeight = noteFont.lineHeight * limitNoteCount + (limitNoteCount - 1) * noteSpace;
if (noteHeight > limitNoteHeight) {
  noteHeight = limitNoteHeight;
  model.noteRowExceedLimit = true;
}
model.noteUnderLimitHeight = noteHeight;

/** 计算笔记高度结束*/
上一篇下一篇

猜你喜欢

热点阅读