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

166 lines
8.1 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.

//
// XPTextLayoutTest.m
// YuMi
//
// Created by Assistant on 2025/8/14.
// Copyright © 2025 YuMi. All rights reserved.
//
#import "XPTextLayoutTest.h"
#import "XPNetImageYYLabel.h"
#import "XPMessageInfoModel.h"
@implementation XPTextLayoutTest
+ (void)testArabicTextLayout {
NSLog(@"=== 测试阿拉伯文本布局 ===");
NSString *arabicText = @"هذا نص عربي طويل لاختبار تخطيط النص. يجب أن يظهر النص بشكل صحيح دون قطع أو مشاكل في العرض.";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:arabicText];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, arabicText.length)];
XPNetImageYYLabel *label = [[XPNetImageYYLabel alloc] init];
[label updateLayoutWithAttributedText:attributedText];
XPMessageInfoModel *model = [[XPMessageInfoModel alloc] init];
model.content = attributedText;
NSLog(@"阿拉伯文本: %@", arabicText);
NSLog(@"文本长度: %lu", (unsigned long)arabicText.length);
NSLog(@"计算的行高: %.2f", model.rowHeight);
}
+ (void)testMixedTextLayout {
NSLog(@"=== 测试混合文本布局 ===");
NSString *mixedText = @"Hello مرحبا 你好 This is mixed text with English, Arabic, and Chinese characters.";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:mixedText];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, mixedText.length)];
XPNetImageYYLabel *label = [[XPNetImageYYLabel alloc] init];
[label updateLayoutWithAttributedText:attributedText];
XPMessageInfoModel *model = [[XPMessageInfoModel alloc] init];
model.content = attributedText;
NSLog(@"混合文本: %@", mixedText);
NSLog(@"文本长度: %lu", (unsigned long)mixedText.length);
NSLog(@"计算的行高: %.2f", model.rowHeight);
}
+ (void)testLongArabicTextLayout {
NSLog(@"=== 测试长阿拉伯文本布局 ===");
NSString *longArabicText = @"هذا نص عربي طويل جداً يحتوي على عدة جمل لاختبار كيفية تعامل نظام تخطيط النص مع النصوص الطويلة. يجب أن يظهر النص بالكامل دون أي قطع أو مشاكل في العرض. هذا اختبار مهم للتأكد من أن النظام يعمل بشكل صحيح مع النصوص العربية الطويلة.";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:longArabicText];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, longArabicText.length)];
XPNetImageYYLabel *label = [[XPNetImageYYLabel alloc] init];
[label updateLayoutWithAttributedText:attributedText];
XPMessageInfoModel *model = [[XPMessageInfoModel alloc] init];
model.content = attributedText;
NSLog(@"长阿拉伯文本: %@", longArabicText);
NSLog(@"文本长度: %lu", (unsigned long)longArabicText.length);
NSLog(@"计算的行高: %.2f", model.rowHeight);
}
+ (void)testChineseTextLayout {
NSLog(@"=== 测试中文文本布局 ===");
NSString *chineseText = @"这是一段很长的中文文本,用来测试文本布局是否正确显示,不会出现截断的问题。这段文本包含了多个句子,应该能够完整显示所有内容,而不会在尾部被意外截断。";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:chineseText];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, chineseText.length)];
XPNetImageYYLabel *label = [[XPNetImageYYLabel alloc] init];
[label updateLayoutWithAttributedText:attributedText];
XPMessageInfoModel *model = [[XPMessageInfoModel alloc] init];
model.content = attributedText;
NSLog(@"中文文本: %@", chineseText);
NSLog(@"文本长度: %lu", (unsigned long)chineseText.length);
NSLog(@"计算的行高: %.2f", model.rowHeight);
}
+ (void)testEnglishTextLayout {
NSLog(@"=== 测试英文文本布局 ===");
NSString *englishText = @"This is a very long English text to test whether the text layout displays correctly without truncation issues. This text contains multiple sentences and should be able to display all content completely without being unexpectedly truncated at the end.";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:englishText];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, englishText.length)];
XPNetImageYYLabel *label = [[XPNetImageYYLabel alloc] init];
[label updateLayoutWithAttributedText:attributedText];
XPMessageInfoModel *model = [[XPMessageInfoModel alloc] init];
model.content = attributedText;
NSLog(@"英文文本: %@", englishText);
NSLog(@"文本长度: %lu", (unsigned long)englishText.length);
NSLog(@"计算的行高: %.2f", model.rowHeight);
}
+ (void)testTurkishTextLayout {
NSLog(@"=== 测试土耳其文文本布局 ===");
NSString *turkishText = @"Bu, metin düzeninin doğru şekilde görüntülenip görüntülenmediğini ve kesme sorunları olmadığını test etmek için çok uzun bir Türkçe metindir. Bu metin birden fazla cümle içerir ve tüm içeriği tamamen görüntüleyebilmeli, sonunda beklenmedik şekilde kesilmemelidir.";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:turkishText];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, turkishText.length)];
XPNetImageYYLabel *label = [[XPNetImageYYLabel alloc] init];
[label updateLayoutWithAttributedText:attributedText];
XPMessageInfoModel *model = [[XPMessageInfoModel alloc] init];
model.content = attributedText;
NSLog(@"土耳其文文本: %@", turkishText);
NSLog(@"文本长度: %lu", (unsigned long)turkishText.length);
NSLog(@"计算的行高: %.2f", model.rowHeight);
}
+ (void)testVeryLongTextLayout {
NSLog(@"=== 测试超长文本布局 ===");
NSString *veryLongText = @"这是一段非常非常长的文本包含了中文、English、العربية、Türkçe等多种语言混合的内容。这段文本的目的是测试在极长文本情况下文本布局系统是否能够正确处理不会出现截断、显示不全或者其他布局问题。文本应该能够完整显示每一个字符都不应该丢失。This text is designed to test the text layout system under extreme conditions with very long content. The system should handle this gracefully without any truncation or display issues. كل حرف يجب أن يظهر بشكل صحيح. Bu metin çok uzun ve karmaşık bir test senaryosudur.";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:veryLongText];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, veryLongText.length)];
XPNetImageYYLabel *label = [[XPNetImageYYLabel alloc] init];
[label updateLayoutWithAttributedText:attributedText];
XPMessageInfoModel *model = [[XPMessageInfoModel alloc] init];
model.content = attributedText;
NSLog(@"超长文本: %@", veryLongText);
NSLog(@"文本长度: %lu", (unsigned long)veryLongText.length);
NSLog(@"计算的行高: %.2f", model.rowHeight);
}
+ (void)runAllTests {
NSLog(@"\n\n=== 开始文本布局测试 ===");
[self testArabicTextLayout];
NSLog(@"\n");
[self testMixedTextLayout];
NSLog(@"\n");
[self testLongArabicTextLayout];
NSLog(@"\n");
[self testChineseTextLayout];
NSLog(@"\n");
[self testEnglishTextLayout];
NSLog(@"\n");
[self testTurkishTextLayout];
NSLog(@"\n");
[self testVeryLongTextLayout];
NSLog(@"\n=== 文本布局测试完成 ===\n\n");
}
@end