尝试修复房间聊天列表显示问题
This commit is contained in:
@@ -26,10 +26,9 @@
|
||||
}
|
||||
|
||||
- (void)setContent:(NSAttributedString *)content {
|
||||
_content = content;
|
||||
|
||||
self.rowHeight = [self heightForAttributedText:content
|
||||
maxWidth:kRoomMessageMaxWidth];
|
||||
_content = content;
|
||||
|
||||
// CGFloat width = isMSRTL() ? 10 : 0;
|
||||
////// width = self.vipIcon.length > 0 ? width + 15 : width;
|
||||
@@ -56,6 +55,17 @@
|
||||
}
|
||||
|
||||
- (CGFloat)heightForAttributedText:(NSAttributedString *)attributedText maxWidth:(CGFloat)maxWidth {
|
||||
|
||||
CGSize containerSize = CGSizeMake(maxWidth, CGFLOAT_MAX);
|
||||
YYTextContainer *_container = [YYTextContainer containerWithSize:containerSize];
|
||||
_container.maximumNumberOfRows = 0; // 不限制行数
|
||||
YYTextLayout *_layout = [YYTextLayout layoutWithContainer:_container text:attributedText];
|
||||
CGFloat textHeight = _layout.textBoundingSize.height;
|
||||
return textHeight + self.contentTopMargin + self.contentBottomMargin + self.cellBottomMargin;
|
||||
|
||||
// return [attributedText.string boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kRoomMessageDefalutFont]} context:nil].size.height;
|
||||
|
||||
|
||||
// 创建一个 YYTextContainer
|
||||
YYTextContainer *container = [YYTextContainer new];
|
||||
container.size = CGSizeMake(maxWidth, CGFLOAT_MAX); // 设置宽度,支持无限高度
|
||||
@@ -63,29 +73,45 @@
|
||||
|
||||
// 遍历富文本,设置未加载图片的占位尺寸
|
||||
// 确认是否 “@用户名” 会影响计算结果
|
||||
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;
|
||||
NSMutableAttributedString *mutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:attributedText.copy];
|
||||
if (isMSRTL()) {
|
||||
NSInteger index = 0;
|
||||
[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 && [attachment.content isKindOfClass:[NetImageView class]]) {
|
||||
NSValue *v = [self.extraSizeArray xpSafeObjectAtIndex:index];
|
||||
NSString *tempString = @"";
|
||||
if (value) {
|
||||
CGSize size = [v CGSizeValue];
|
||||
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
|
||||
// attachment.content = view;
|
||||
if (size.width <= 20) {
|
||||
tempString = @"ABCD";
|
||||
} else if (size.width <=40) {
|
||||
tempString = @"ABCDEF";
|
||||
} else {
|
||||
tempString = @"ABCDEFGH";
|
||||
}
|
||||
}
|
||||
|
||||
NSAttributedString *replaceString = [[NSAttributedString alloc] initWithString:tempString
|
||||
attributes:@{
|
||||
NSFontAttributeName: kFontRegular(kRoomMessageDefalutFont),
|
||||
NSForegroundColorAttributeName: [UIColor whiteColor]
|
||||
}];
|
||||
[mutableAttributedText replaceCharactersInRange:range withAttributedString:replaceString];
|
||||
}
|
||||
}
|
||||
}
|
||||
}];
|
||||
}];
|
||||
}
|
||||
|
||||
// 通过 YYTextLayout 创建布局
|
||||
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:mutableAttributedText];
|
||||
|
Reference in New Issue
Block a user