Files
peko-ios/YuMi/Modules/YMRoom/View/MessageContainerView/Model/XPMessageInfoModel.m
edwinQQQ b1e46f6d28 fix(消息容器): 修复多语言文本截断问题并优化布局
统一文本布局策略,修复阿拉伯语、中文等文本显示不全问题
优化约束逻辑,移除冗余计算,提升性能
添加测试用例验证多语言文本布局
2025-08-14 17:56:56 +08:00

94 lines
2.6 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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 {
_content = content;
if (self.first == 9 && self.second == 91) {
self.rowHeight = 100;
return;
}
if (self.isBoom) {
self.rowHeight = kGetScaleWidth(60)+6+6+20;
return;
}
// 减去 contentLabel 的左右边距 (UIEdgeInsetsMake(4, 12, 4, 12) 中的左右各12px)
CGFloat width = kRoomMessageMaxWidth - 24;
if (self.isBoom) {
width = kRoomMessageMaxWidth - 24 - kGetScaleWidth(60);
}
YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(width, CGFLOAT_MAX)];
container.maximumNumberOfRows = 0;
// 根据文本方向配置容器
if (isMSRTL()) {
container.truncationType = YYTextTruncationTypeStart;
} else {
container.truncationType = YYTextTruncationTypeEnd;
}
container.insets = UIEdgeInsetsMake(self.contentTopMargin, self.contentLeftMargin, self.contentBottomMargin, self.contentLeftMargin);
YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:content];
CGSize textSize = textLayout.textBoundingRect.size;
self.textWidth = textSize.width;
self.rowHeight = textSize.height;
// if ([NSString isEmpty:self.bubbleImageUrl]) {
self.rowHeight =
ceil(textSize.height) +
self.cellBottomMargin +
self.contentTopMargin;
// (isMSRTL() ? self.contentTopMargin : (self.isBoom ? 20 : 8));
// } else {
// self.rowHeight =
// ceil(textSize.height) +
// (isMSRTL() ? self.contentTopMargin : (self.isBoom ? 20 : 8)) +
//// self.contentBottomMargin +
// self.cellBottomMargin;
// }
self.rowHeight += 10;
self.rowHeight += (self.isBoom ? 20 : 0);
// 移除额外的阿拉伯文本高度调整YYTextLayout 已能正确处理 RTL 文本
}
- (BOOL)isStringContainArabic:(NSString *)string {
for (NSUInteger i = 0; i < string.length; i++) {
unichar c = [string characterAtIndex:i];
if (c >= 0x0600 && c <= 0x06FF) {
return YES;
}
}
return NO;
}
@end