日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UITableView的动态获取高度 排版

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的动态获取高度 排版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。