Skip to content

Commit

Permalink
fix(android): empty text node height compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
iPel authored and siguangli committed Nov 7, 2022
1 parent c3bc20c commit f9bf570
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public class TextNode extends StyleNode {
public static final String IMAGE_SPAN_TEXT = "[img]";

final TextPaint mTextPaintInstance;
// 这个TextPaint用于兼容2.13.x及之前版本对于空Text节点的layout高度
private TextPaint mTextPaintForEmpty;

private final boolean mIsVirtual;

Expand Down Expand Up @@ -738,7 +740,7 @@ private StaticLayout buildStaticLayout(CharSequence source, TextPaint paint, int
}

protected Layout createLayout(float width, FlexMeasureMode widthMode) {
TextPaint textPaint = mTextPaintInstance;
final TextPaint textPaint = getTextPaint();
Layout layout;
Spanned text = mSpanned == null ? new SpannedString("") : mSpanned;
BoringLayout.Metrics boring = null;
Expand Down Expand Up @@ -773,6 +775,16 @@ protected Layout createLayout(float width, FlexMeasureMode widthMode) {
return layout;
}

private TextPaint getTextPaint() {
if (TextUtils.isEmpty(mText)) {
if (mTextPaintForEmpty == null) {
mTextPaintForEmpty = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
}
return mTextPaintForEmpty;
}
return mTextPaintInstance;
}

private StaticLayout truncateLayoutWithNumberOfLine(Layout preLayout, int width, int numberOfLines) {
int lineCount = preLayout.getLineCount();
assert lineCount >= 2;
Expand Down Expand Up @@ -810,7 +822,9 @@ private StaticLayout truncateLayoutWithNumberOfLine(Layout preLayout, int width,
}
CharSequence lastLine;
if (MODE_HEAD.equals(mEllipsizeMode)) {
measurePaint.setTextSize(Math.max(getLineHeight(preLayout, lineCount - 2), getLineHeight(preLayout, lineCount - 1)));
float formerTextSize = numberOfLines >= 2 ? getLineHeight(preLayout, numberOfLines - 2) : paint.getTextSize();
float latterTextSize = Math.max(getLineHeight(preLayout, lineCount - 2), getLineHeight(preLayout, lineCount - 1));
measurePaint.setTextSize(Math.max(formerTextSize, latterTextSize));
lastLine = ellipsizeHead(origin, measurePaint, width, start);
} else if (MODE_MIDDLE.equals(mEllipsizeMode)) {
measurePaint.setTextSize(Math.max(getLineHeight(preLayout, numberOfLines - 1), getLineHeight(preLayout, lineCount - 1)));
Expand Down

0 comments on commit f9bf570

Please sign in to comment.