背景:最近AI對(duì)話(huà)比較熱門(mén),AI回復(fù)的內(nèi)容有很多markdown格式,需要展示一下。
三方庫(kù):CocoaMarkdown
在github上搜索并下載源碼到本地,打開(kāi)工程文件,選擇Any iOS Device編譯成功后,在左側(cè)文件欄最下面有一個(gè)Products目錄,展開(kāi)找到CocoaMarkdown.framework,將此文件復(fù)制到自己的工程,這里是真機(jī)的,需要使用模擬器的自己選擇模擬器編譯進(jìn)行后再合并,這里不敘述。
下面是關(guān)鍵代碼,計(jì)算高度
// 導(dǎo)入
#import <CocoaMarkdown/CocoaMarkdown.h>
self.aiResWidth = k_screen_width - 55 - 26 - 10;
DLog(@"self.dadaAiResWidth = %f",self.aiResWidth);
CGSize maxSize = CGSizeMake(self.aiResWidth, 100000);
// 1.字符串轉(zhuǎn)data
NSData *data = [self.body dataUsingEncoding:NSUTF8StringEncoding];
// 2. 渲染成 NSAttributedString
CMDocument *doc = [[CMDocument alloc] initWithData:data options:CMDocumentOptionsSmart];
// 3. 創(chuàng)建屬性對(duì)象
CMTextAttributes *attrs = [[CMTextAttributes alloc] init];
[attrs addStringAttributes:@{NSFontAttributeName:kFont14} forElementWithKinds:CMElementKindAnyHeader];
[attrs addStringAttributes:@{NSFontAttributeName:kFont13} forElementWithKinds:CMElementKindParagraph];
[attrs addStringAttributes:@{NSFontAttributeName:kFont13} forElementWithKinds:CMElementKindText];
CMAttributedStringRenderer *renderer = [[CMAttributedStringRenderer alloc] initWithDocument:doc
attributes:attrs];
NSAttributedString *attrStr = [renderer render];
[self setAttrStr:attrStr];
// 4. 計(jì)算高度,給定最大寬度(例如 tableView 的 contentWidth)
CGRect boundingRect = [attrStr boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
self.aiResHeight = ceil(boundingRect.size.height);
下面是關(guān)鍵代碼,使用UITextView進(jìn)行渲染
- (UITextView *)body {
if (!_body) {
CGRect rect = CGRectMake(16, 10, 197, 80);
_body = [[UITextView alloc] initWithFrame:rect];
_body.font = kFont13;
_body.textColor = kBlackColorD;
_body.editable = NO;
_body.scrollEnabled = NO; // 如果外層有 UIScrollView/UITableView 就關(guān)掉
_body.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);// 去掉上下默認(rèn)8pt邊距
_body.textContainer.lineFragmentPadding = 0; // 去掉左右 5 pt 的邊距
[_body setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisVertical];
}
return _body;
}
// 賦值顯示
self.body.attributedText = att.attrStr;
// 設(shè)置位置
self.body.frame = CGRectMake(16, 12, att.aiResWidth, att.aiResHeight);
注:去掉邊距比較重要, 不然顯示可能會(huì)少一行。