publicstaticgetShortText(textSectionAttribute: TextSectionAttribute, lastSpan: string): string {
let text = TextUtils.getStringFromResource(textSectionAttribute.title);
constminLinesTextSize: SizeOptions | undefined = uiContext?.getMeasureUtils().measureTextSize({
textContent: text,
fontSize: textSectionAttribute.fontSize,
maxLines: textSectionAttribute.maxLines,
wordBreak: WordBreak.BREAK_ALL,
constraintWidth: textSectionAttribute.constraintWidth
});
constminHeight: Length | undefined = minLinesTextSize?.height;
if (minHeight === undefined) {
return'';
}
// Use the dichotomy to find strings that are exactly two lines in lengthlettextStr: string[] = Array.from(text); // Split the string to avoid special characters and inconsistent sizesletleftCursor: number = 0;
letrightCursor: number = textStr.length;
letcursor: number = Math.floor(rightCursor / 2);
lettempTitle: string = '';
while (true) {
tempTitle = text.substring(0, cursor) + suffix + lastSpan;
constcurrentLinesTextSize: SizeOptions | undefined = uiContext?.getMeasureUtils().measureTextSize({
textContent: tempTitle,
fontSize: textSectionAttribute.fontSize,
wordBreak: WordBreak.BREAK_ALL,
constraintWidth: textSectionAttribute.constraintWidth
});
constcurrentLineHeight: Length | undefined = currentLinesTextSize?.height;
if (currentLineHeight === undefined) {
return'';
}
if (currentLineHeight > minHeight) {
// The current character has exceeded two lines, continue to look to the left
rightCursor = cursor;
cursor = leftCursor + Math.floor((cursor - leftCursor) / 2);
} else {
// The current character is less than two lines, it may be OK, but you still need to look to the right
leftCursor = cursor;
cursor += Math.floor((rightCursor - cursor) / 2);
}
if (Math.abs(rightCursor - leftCursor) <= 1) {
// The two pointers basically coincide, which means that they have been foundbreak;
}
}
return text.substring(0, cursor) + suffix;
}
转换坐标对应索引。计算 lastWord 的展示索引位置。拿到 lastWord 的 x 与 y 坐标之后,通过 getGlyphPositionAtCoordinate() 拿到这个坐标的文字所在段落的索引,这个就是最终文字展示的索引。
// The conversion coordinates correspond to the indexlet positionWithAffinity = paragraph.getGlyphPositionAtCoordinate(x, y);
let index = 0;
if (positionWithAffinity.affinity === text.Affinity.UPSTREAM) {
index = positionWithAffinity.position;
} else {
index = positionWithAffinity.position + 1;
}
计算 lastWord 的 X 坐标。
if (paragraph.getLineWidth(textSectionAttribute.maxLines - 1) + Number(widthMore) > textSectionAttribute.constraintWidth) {
x = textSectionAttribute.constraintWidth - Number(widthMore);
} else {
x = paragraph.getLineWidth(textSectionAttribute.maxLines - 1);
}
for (let i = 0; i < textSectionAttribute.maxLines; i++) {
y += i === textSectionAttribute.maxLines - 1 ? paragraph.getLineHeight(i) / 2 : paragraph.getLineHeight(i);
}
预排版。全部添加完成之后,使用 paragraph 的 layoutSync() 方法预先排版,传递的大小单位也为 px。这个 layoutSync() 传递宽度要同展示的时候的 Text 文本宽度一致,否则计算出来的和展示的时候肯定不一致。
let paragraph = paragraphGraphBuilder.build();
paragraph.layoutSync(textMaxWidth);
letmyTextStyle: text.TextStyle = { fontSize: uiContext?.fp2px(fontSize) };
letmyParagraphStyle: text.ParagraphStyle = {
textStyle: myTextStyle,
align: text.TextAlign.START,
maxLines: 300, // Just specify a large enough number of rowsbreakStrategy: text.BreakStrategy.GREEDY,
wordBreak: text.WordBreak.BREAK_WORD
};
let fontCollection = new text.FontCollection();
let paragraphGraphBuilder = new text.ParagraphBuilder(myParagraphStyle, fontCollection);