126 lines
5.8 KiB
Objective-C
126 lines
5.8 KiB
Objective-C
//
|
|
// YMMessageInfoModel.m
|
|
// YUMI
|
|
//
|
|
// Created by YUMI on 2022/10/21.
|
|
//
|
|
|
|
#import "XPMessageInfoModel.h"
|
|
#import "XPRoomMessageConstant.h"
|
|
|
|
|
|
|
|
@implementation XPMessageInfoModel
|
|
|
|
- (instancetype)init {
|
|
if ([super init]) {
|
|
self.contentLeftMargin = 12;
|
|
self.contentRightMargin = 0;
|
|
self.contentTopMargin = 10;
|
|
self.contentBottomMargin = 10;
|
|
self.cellBottomMargin = 6;
|
|
self.isUpdateContentFrame = NO;
|
|
self.isHiddenBubble = NO;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)setContent:(NSAttributedString *)content {
|
|
self.rowHeight = [self heightForAttributedText:content
|
|
maxWidth:kRoomMessageMaxWidth];
|
|
_content = content;
|
|
|
|
// CGFloat width = isMSRTL() ? 10 : 0;
|
|
////// 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;
|
|
// YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:self.content];
|
|
// CGFloat rowHeight = layout.textBoundingSize.height + self.contentTopMargin + self.contentBottomMargin + self.cellBottomMargin;
|
|
// 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 {
|
|
|
|
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); // 设置宽度,支持无限高度
|
|
container.maximumNumberOfRows = 0; // 允许多行
|
|
|
|
// 遍历富文本,设置未加载图片的占位尺寸
|
|
// 确认是否 “@用户名” 会影响计算结果
|
|
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];
|
|
|
|
self.textWidth = ceil(layout.textBoundingSize.width);
|
|
|
|
// 返回布局的文本高度
|
|
return ceil(layout.textBoundingSize.height) + self.contentTopMargin + self.contentBottomMargin + self.cellBottomMargin; // 向上取整,避免内容裁切
|
|
}
|
|
|
|
@end
|