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 5b3aee3
Showing 1 changed file with 31 additions and 3 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 @@ -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()) {
Expand Down Expand Up @@ -697,16 +718,23 @@ 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
: formerLines instanceof SpannableStringBuilder
? ((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) {
Expand Down

0 comments on commit 5b3aee3

Please sign in to comment.