Fix line wrap for EmojiTextViews with ImageSpans.
This commit is contained in:
parent
a0d70a955a
commit
85b3947150
1 changed files with 15 additions and 4 deletions
|
@ -13,6 +13,7 @@ import android.text.TextDirectionHeuristic;
|
|||
import android.text.TextDirectionHeuristics;
|
||||
import android.text.TextUtils;
|
||||
import android.text.method.TransformationMethod;
|
||||
import android.text.style.MetricAffectingSpan;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -193,8 +194,10 @@ public class EmojiTextView extends AppCompatTextView {
|
|||
if (text != null) {
|
||||
int widthSpecMode = MeasureSpec.getMode(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) {
|
||||
return MeasureSpec.makeMeasureSpec(desiredWidth + 1, MeasureSpec.EXACTLY);
|
||||
}
|
||||
|
@ -204,6 +207,14 @@ public class EmojiTextView extends AppCompatTextView {
|
|||
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() {
|
||||
return lastLineWidth;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue