Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): add buildTruncateAtEndStaticLayout impl #3988

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -557,16 +558,20 @@ protected Layout createLayout(final float width, final FlexMeasureMode widthMode
|| desiredWidth > width)) {
desiredWidth = width;
}
layout = buildStaticLayout(mSpanned, textPaint, (int) Math.ceil(desiredWidth));
if (mNumberOfLines > 0 && layout.getLineCount() > mNumberOfLines) {
int lastLineStart = layout.getLineStart(mNumberOfLines - 1);
int lastLineEnd = layout.getLineEnd(mNumberOfLines - 1);
if (lastLineStart < lastLineEnd) {
int measureWidth = (int) Math.ceil(unconstrainedWidth ? desiredWidth : width);
try {
layout = truncateLayoutWithNumberOfLine(layout, measureWidth, mNumberOfLines);
} catch (Exception e) {
e.printStackTrace();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && MODE_TAIL.equals(mEllipsizeMode)) {
layout = buildTruncateAtEndStaticLayout(mSpanned, textPaint, (int) Math.ceil(desiredWidth), mNumberOfLines);
} else {
layout = buildStaticLayout(mSpanned, textPaint, (int) Math.ceil(desiredWidth));
if (mNumberOfLines > 0 && layout.getLineCount() > mNumberOfLines) {
int lastLineStart = layout.getLineStart(mNumberOfLines - 1);
int lastLineEnd = layout.getLineEnd(mNumberOfLines - 1);
if (lastLineStart < lastLineEnd) {
int measureWidth = (int) Math.ceil(unconstrainedWidth ? desiredWidth : width);
try {
layout = truncateLayoutWithNumberOfLine(layout, measureWidth, mNumberOfLines);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Expand Down Expand Up @@ -618,6 +623,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()) {
Expand Down Expand Up @@ -705,7 +730,6 @@ private StaticLayout truncateLayoutWithNumberOfLine(Layout preLayout, int width,
? ((SpannableStringBuilder) formerLines).append(lastLine)
: ((StringBuilder) formerLines).append(lastLine);
}

return buildStaticLayout(truncated, paint, width);
}

Expand Down
Loading