From 5b3aee37f7fad17b7c97f1690e1074a8e6f9c1dd Mon Sep 17 00:00:00 2001 From: maxli Date: Wed, 7 Aug 2024 17:04:49 +0800 Subject: [PATCH] fix(android): add buildTruncateAtEndStaticLayout impl --- .../renderer/node/TextVirtualNode.java | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/node/TextVirtualNode.java b/renderer/native/android/src/main/java/com/tencent/renderer/node/TextVirtualNode.java index 238d78f6955..14520e3692e 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/node/TextVirtualNode.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/node/TextVirtualNode.java @@ -31,6 +31,7 @@ import android.text.StaticLayout; import android.text.TextPaint; import android.text.TextUtils; +import android.text.TextUtils.TruncateAt; import android.text.style.AbsoluteSizeSpan; import android.text.style.BackgroundColorSpan; import android.text.style.ImageSpan; @@ -618,6 +619,26 @@ private TextPaint getTextPaint() { return mTextPaintInstance; } + @RequiresApi(api = Build.VERSION_CODES.M) + private StaticLayout buildTruncateAtEndStaticLayout(CharSequence source, TextPaint paint, int width, int numberOfLines) { + Layout.Alignment alignment = mAlignment; + if (I18nUtil.isRTL()) { + BidiFormatter bidiFormatter = BidiFormatter.getInstance(); + if (bidiFormatter.isRtl(source.toString()) + && mAlignment == Layout.Alignment.ALIGN_OPPOSITE) { + alignment = Layout.Alignment.ALIGN_NORMAL; + } + } + return StaticLayout.Builder.obtain(source, 0, source.length(), paint, width) + .setAlignment(alignment) + .setLineSpacing(mLineSpacingExtra, getLineSpacingMultiplier()) + .setIncludePad(true) + .setMaxLines(numberOfLines) + .setEllipsize(TruncateAt.END) + .setBreakStrategy(getBreakStrategy()) + .build(); + } + private StaticLayout buildStaticLayout(CharSequence source, TextPaint paint, int width) { Layout.Alignment alignment = mAlignment; if (I18nUtil.isRTL()) { @@ -697,7 +718,11 @@ private StaticLayout truncateLayoutWithNumberOfLine(Layout preLayout, int width, lastLine = ellipsizeMiddle(origin, measurePaint, width, start, preLayout.getLineEnd(numberOfLines), preLayout.getLineStart(lineCount - 2)); } else /*if (MODE_TAIL.equals(mEllipsizeMode))*/ { - lastLine = ellipsizeTail(origin, measurePaint, width, start, preLayout.getLineEnd(numberOfLines)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + lastLine = origin; + } else { + lastLine = ellipsizeTail(origin, measurePaint, width, start, preLayout.getLineEnd(numberOfLines)); + } } // concat everything truncated = formerLines == null ? lastLine @@ -705,8 +730,11 @@ private StaticLayout truncateLayoutWithNumberOfLine(Layout preLayout, int width, ? ((SpannableStringBuilder) formerLines).append(lastLine) : ((StringBuilder) formerLines).append(lastLine); } - - return buildStaticLayout(truncated, paint, width); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && MODE_TAIL.equals(mEllipsizeMode)) { + return buildTruncateAtEndStaticLayout(truncated, paint, width, numberOfLines); + } else { + return buildStaticLayout(truncated, paint, width); + } } private float chooseTextSize(float inherit, CharSequence text) {