Skip to content

Commit

Permalink
fix(android): add buildTruncateAtEndStaticLayout impl
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Aug 7, 2024
1 parent 4b2506e commit 84a27d8
Showing 1 changed file with 35 additions and 11 deletions.
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

0 comments on commit 84a27d8

Please sign in to comment.