|
18 | 18 | import android.text.TextUtils;
|
19 | 19 | import android.text.method.LinkMovementMethod;
|
20 | 20 | import android.text.util.Linkify;
|
| 21 | +import android.util.TypedValue; |
21 | 22 | import android.view.Gravity;
|
22 | 23 | import android.view.MotionEvent;
|
23 | 24 | import android.view.View;
|
@@ -57,6 +58,8 @@ public class ReactTextView extends AppCompatTextView implements ReactCompoundVie
|
57 | 58 | private int mNumberOfLines;
|
58 | 59 | private TextUtils.TruncateAt mEllipsizeLocation;
|
59 | 60 | private boolean mAdjustsFontSizeToFit;
|
| 61 | + private float mFontSize = Float.NaN; |
| 62 | + private float mLetterSpacing = Float.NaN; |
60 | 63 | private int mLinkifyMaskType;
|
61 | 64 | private boolean mNotifyOnInlineViewLayout;
|
62 | 65 | private boolean mTextIsSelectable;
|
@@ -582,6 +585,29 @@ public void setAdjustFontSizeToFit(boolean adjustsFontSizeToFit) {
|
582 | 585 | mAdjustsFontSizeToFit = adjustsFontSizeToFit;
|
583 | 586 | }
|
584 | 587 |
|
| 588 | + public void setFontSize(float fontSize) { |
| 589 | + mFontSize = |
| 590 | + mAdjustsFontSizeToFit |
| 591 | + ? (float) Math.ceil(PixelUtil.toPixelFromSP(fontSize)) |
| 592 | + : (float) Math.ceil(PixelUtil.toPixelFromDIP(fontSize)); |
| 593 | + |
| 594 | + applyTextAttributes(); |
| 595 | + } |
| 596 | + |
| 597 | + public void setLetterSpacing(float letterSpacing) { |
| 598 | + if (Float.isNaN(letterSpacing)) { |
| 599 | + return; |
| 600 | + } |
| 601 | + |
| 602 | + float letterSpacingPixels = PixelUtil.toPixelFromDIP(letterSpacing); |
| 603 | + |
| 604 | + // `letterSpacingPixels` and `getEffectiveFontSize` are both in pixels, |
| 605 | + // yielding an accurate em value. |
| 606 | + mLetterSpacing = letterSpacingPixels / mFontSize; |
| 607 | + |
| 608 | + applyTextAttributes(); |
| 609 | + } |
| 610 | + |
585 | 611 | public void setEllipsizeLocation(TextUtils.TruncateAt ellipsizeLocation) {
|
586 | 612 | mEllipsizeLocation = ellipsizeLocation;
|
587 | 613 | }
|
@@ -651,4 +677,17 @@ protected boolean dispatchHoverEvent(MotionEvent event) {
|
651 | 677 |
|
652 | 678 | return super.dispatchHoverEvent(event);
|
653 | 679 | }
|
| 680 | + |
| 681 | + private void applyTextAttributes() { |
| 682 | + // Workaround for an issue where text can be cut off with an ellipsis when |
| 683 | + // using certain font sizes and padding. Sets the provided text size and |
| 684 | + // letter spacing to ensure consistent rendering and prevent cut-off. |
| 685 | + if (!Float.isNaN(mFontSize)) { |
| 686 | + setTextSize(TypedValue.COMPLEX_UNIT_PX, mFontSize); |
| 687 | + } |
| 688 | + |
| 689 | + if (!Float.isNaN(mLetterSpacing)) { |
| 690 | + super.setLetterSpacing(mLetterSpacing); |
| 691 | + } |
| 692 | + } |
654 | 693 | }
|
0 commit comments