feat:修正需求验收问题
This commit is contained in:
@@ -27,8 +27,12 @@
|
||||
|
||||
- (void)setContent:(NSAttributedString *)content {
|
||||
_content = content;
|
||||
|
||||
self.rowHeight = [self heightForAttributedText:content
|
||||
maxWidth:kRoomMessageMaxWidth];
|
||||
|
||||
// CGFloat width = isMSRTL() ? 10 : 0;
|
||||
//// width = self.vipIcon.length > 0 ? width + 15 : width;
|
||||
////// width = self.vipIcon.length > 0 ? width + 15 : width;
|
||||
// CGSize maxSize = CGSizeMake(kRoomMessageMaxWidth - self.contentLeftMargin - self.contentRightMargin - 12, MAXFLOAT);
|
||||
// YYTextContainer *container = [YYTextContainer containerWithSize:maxSize];
|
||||
// container.maximumNumberOfRows = 0;
|
||||
@@ -37,4 +41,59 @@
|
||||
// self.rowHeight = rowHeight + width ;
|
||||
}
|
||||
|
||||
- (CGFloat)heightForAttributedStringUsingTextKit:(NSAttributedString *)attributedString
|
||||
maxWidth:(CGFloat)maxWidth {
|
||||
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
|
||||
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
|
||||
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
|
||||
|
||||
[layoutManager addTextContainer:textContainer];
|
||||
[textStorage addLayoutManager:layoutManager];
|
||||
|
||||
// 让布局管理器计算文本高度
|
||||
[layoutManager glyphRangeForTextContainer:textContainer];
|
||||
return ceil([layoutManager usedRectForTextContainer:textContainer].size.height); // 向上取整
|
||||
}
|
||||
|
||||
- (CGFloat)heightForAttributedText:(NSAttributedString *)attributedText maxWidth:(CGFloat)maxWidth {
|
||||
// 创建一个 YYTextContainer
|
||||
YYTextContainer *container = [YYTextContainer new];
|
||||
container.size = CGSizeMake(maxWidth, CGFLOAT_MAX); // 设置宽度,支持无限高度
|
||||
container.maximumNumberOfRows = 0; // 允许多行
|
||||
|
||||
// 遍历富文本,设置未加载图片的占位尺寸
|
||||
// 确认是否 “@用户名” 会影响计算结果
|
||||
NSInteger index = 0;
|
||||
NSMutableAttributedString *mutableAttributedText = [attributedText mutableCopy];
|
||||
[mutableAttributedText enumerateAttribute:YYTextAttachmentAttributeName
|
||||
inRange:NSMakeRange(0, mutableAttributedText.length)
|
||||
options:0
|
||||
usingBlock:^(id value, NSRange range, BOOL *stop) {
|
||||
|
||||
if ([value isKindOfClass:[YYTextAttachment class]]) {
|
||||
// TODO: 把图片变为字符串来占位, 观察结果是第二行字符长度少于前缀图片,则会高度减少
|
||||
YYTextAttachment *attachment = (YYTextAttachment *)value;
|
||||
NSLog(@" ---00--- : %@", attachment.content);
|
||||
if (attachment.content == nil) {
|
||||
attachment.content = [[UIView alloc] init]; // 占位内容
|
||||
NSValue *v = [self.extraSizeArray xpSafeObjectAtIndex:index];
|
||||
if (value) {
|
||||
UIView *placeholderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [v CGSizeValue].width, [v CGSizeValue].height)];
|
||||
attachment.content = placeholderView; // 使用占位尺寸
|
||||
} else {
|
||||
*stop = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
}];
|
||||
|
||||
// 通过 YYTextLayout 创建布局
|
||||
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:mutableAttributedText];
|
||||
|
||||
self.textWidth = ceil(layout.textBoundingSize.width);
|
||||
|
||||
// 返回布局的文本高度
|
||||
return ceil(layout.textBoundingSize.height) + self.contentTopMargin + self.contentBottomMargin + self.cellBottomMargin; // 向上取整,避免内容裁切
|
||||
}
|
||||
|
||||
@end
|
||||
|
Reference in New Issue
Block a user