UITableView的动态获取高度 排版
? 大神請繞路~~~?
最近做一款應用的界面,頁面是圖片,文字,頭像,地址,時間等隨機排列組成的。
這里簡單記錄,解決文字單獨排版的解決方法,看了網上很多大神的博客分享內容。
剛開始,使用了最常見的方法:
?在?cellForRowAtIndexPath 里面 賦值文字顯示,
在 heightForRowAtIndexPath 里面使用Category如下:
?
- (CGSize)boundingRectWithSize:(CGSize)size
{
? ? NSDictionary *attribute = @{NSFontAttributeName: self.font};
?? ?
? ? CGSize retSize = [self.text boundingRectWithSize:size
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options:\
? ? ? ? ? ? ? ? ? ? ? NSStringDrawingTruncatesLastVisibleLine |
? ? ? ? ? ? ? ? ? ? ? NSStringDrawingUsesLineFragmentOrigin |
? ? ? ? ? ? ? ? ? ? ? NSStringDrawingUsesFontLeading
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes:attribute
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? context:nil].size;
?? ?
? ? return retSize;
}
? 計算出這段文字的高度,加上cell上其他圖片之類的高度,返回高度值。
應為cell上面的label是約束搞得 ,所以改變了cell得高度,label高度自然改變。
?
?
后來,設計師要求更改文字行間距,查詢N久,解決方法如下:
? ? ? ? ? ??NSString *cntentText = @“水電費水電費的說法”;
? ? ? ? ? ? if (cntentText) {
? ? ? ? ? ? ? ? NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:cntentText];;
? ? ? ? ? ? ? ? NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
? ? ? ? ? ? ? ? [paragraphStyle setLineSpacing:15];//行間距
? ? ? ? ? ? ? ? [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, cntentText.length)];
? ? ? ? ? ? ? ? cell.concentText.attributedText = attributedString;
? ? ? ? ? ? }
?
之后需要在?heightForRowAtIndexPath 中計算加了行間距此段文字的高度:
? ? ? ? ? ? ? ? NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:detailContent];;
? ? ? ? ? ? ? ? NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
? ? ? ? ? ? ? ? [paragraphStyle setLineSpacing:15];//和上面對應
? ? ? ? ? ? ? ? [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, detailContent.length)];
? ? ? ? ? ? ? ? cell.concentText.attributedText = attributedString;
? ? ? ? ? ? ? ? CGSize size1 = CGSizeMake(kDECEIVE_WIDTH - 16 - 16, 5000);
? ? ? ? ? ? ? ? CGSize labelSize = [cell.concentText sizeThatFits:size1];
? ? ? ? ? ? ? ? return labelSize.height + 20;//加上其他內容占據的空間大小返回
?
?
暫時解決,會繼續想其他辦法看能否簡化下,可以需要支持iOS7,不能使用iOS8的cell自適應大小。
繼續搬磚解決字間距段間距的問題。
?
?
轉載于:https://www.cnblogs.com/iOS-xiao-lei/p/4466225.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的UITableView的动态获取高度 排版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二级联动菜单(javascript)
- 下一篇: C++ 术语(C++ Primer)