Fix line wrap for EmojiTextViews with ImageSpans.

This commit is contained in:
Rashad Sookram 2021-12-22 12:31:11 -05:00 committed by Greyson Parrelli
parent a0d70a955a
commit 85b3947150

View file

@ -13,6 +13,7 @@ import android.text.TextDirectionHeuristic;
import android.text.TextDirectionHeuristics; import android.text.TextDirectionHeuristics;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.method.TransformationMethod; import android.text.method.TransformationMethod;
import android.text.style.MetricAffectingSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -191,10 +192,12 @@ public class EmojiTextView extends AppCompatTextView {
if (Build.VERSION.SDK_INT >= 30 && Math.abs(getResources().getConfiguration().fontScale - 1f) > 0.01f) { if (Build.VERSION.SDK_INT >= 30 && Math.abs(getResources().getConfiguration().fontScale - 1f) > 0.01f) {
CharSequence text = getText(); CharSequence text = getText();
if (text != null) { if (text != null) {
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int measuredTextWidth = (int) getPaint().measureText(text, 0, text.length());
int desiredWidth = measuredTextWidth + getPaddingLeft() + getPaddingRight(); float measuredTextWidth = hasMetricAffectingSpan(text) ? Layout.getDesiredWidth(text, getPaint()) : getPaint().measureText(text, 0, text.length());
int desiredWidth = (int) measuredTextWidth + getPaddingLeft() + getPaddingRight();
if (widthSpecMode == MeasureSpec.AT_MOST && desiredWidth < widthSpecSize) { if (widthSpecMode == MeasureSpec.AT_MOST && desiredWidth < widthSpecSize) {
return MeasureSpec.makeMeasureSpec(desiredWidth + 1, MeasureSpec.EXACTLY); return MeasureSpec.makeMeasureSpec(desiredWidth + 1, MeasureSpec.EXACTLY);
} }
@ -204,6 +207,14 @@ public class EmojiTextView extends AppCompatTextView {
return widthMeasureSpec; return widthMeasureSpec;
} }
private boolean hasMetricAffectingSpan(@NonNull CharSequence text) {
if (!(text instanceof Spanned)) {
return false;
}
return ((Spanned) text).nextSpanTransition(-1, text.length(), MetricAffectingSpan.class) != text.length();
}
public int getLastLineWidth() { public int getLastLineWidth() {
return lastLineWidth; return lastLineWidth;
} }