Skip to content

Commit 78836da

Browse files
Implement minimumFontScale, match ios behavior
1 parent 620502c commit 78836da

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewProps.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public class ViewProps {
8989
public static final String NUMBER_OF_LINES = "numberOfLines";
9090
public static final String ELLIPSIZE_MODE = "ellipsizeMode";
9191
public static final String ADJUSTS_FONT_SIZE_TO_FIT = "adjustsFontSizeToFit";
92+
public static final String MINIMUM_FONT_SCALE = "minimumFontScale";
9293
public static final String ON = "on";
9394
public static final String RESIZE_MODE = "resizeMode";
9495
public static final String RESIZE_METHOD = "resizeMethod";

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ private static int parseNumericFontWeight(String fontWeightString) {
342342
protected boolean mIsLineThroughTextDecorationSet = false;
343343
protected boolean mIncludeFontPadding = true;
344344
protected boolean mAdjustsFontSizeToFit = false;
345+
protected float mMinimumFontScale = 0;
345346

346347
/**
347348
* mFontStyle can be {@link Typeface#NORMAL} or {@link Typeface#ITALIC}. mFontWeight can be {@link
@@ -630,4 +631,12 @@ public void setAdjustFontSizeToFit(boolean adjustsFontSizeToFit) {
630631
markUpdated();
631632
}
632633
}
634+
635+
@ReactProp(name = ViewProps.MINIMUM_FONT_SCALE)
636+
public void setMinimumFontScale(float minimumFontScale) {
637+
if (minimumFontScale != mMinimumFontScale) {
638+
mMinimumFontScale = minimumFontScale;
639+
markUpdated();
640+
}
641+
}
633642
}

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.facebook.react.bridge.WritableArray;
2323
import com.facebook.react.bridge.WritableMap;
2424
import com.facebook.react.uimanager.NativeViewHierarchyOptimizer;
25+
import com.facebook.react.uimanager.PixelUtil;
2526
import com.facebook.react.uimanager.ReactShadowNode;
2627
import com.facebook.react.uimanager.Spacing;
2728
import com.facebook.react.uimanager.UIViewOperationQueue;
@@ -161,14 +162,24 @@ public long measure(
161162
Layout layout = measureSpannedText(text, width, widthMode);
162163

163164
if (mAdjustsFontSizeToFit) {
165+
int initialFontSize = mTextAttributes.getEffectiveFontSize();
166+
int currentFontSize = mTextAttributes.getEffectiveFontSize();
167+
// Minimum font size is 4pts to match the iOS implementation.
168+
int minimumFontSize = (int) Math.max(mMinimumFontScale * initialFontSize, PixelUtil.toPixelFromDIP(4));
164169
while (
165-
mNumberOfLines != UNSET && layout.getLineCount() > mNumberOfLines ||
166-
heightMode != YogaMeasureMode.UNDEFINED && layout.getHeight() > height
170+
currentFontSize > minimumFontSize && (
171+
mNumberOfLines != UNSET && layout.getLineCount() > mNumberOfLines ||
172+
heightMode != YogaMeasureMode.UNDEFINED && layout.getHeight() > height)
167173
) {
174+
// TODO: We could probably use a smarter algorithm here. This will require 0(n) measurements
175+
// based on the number of points the font size needs to be reduced by.
176+
currentFontSize = currentFontSize - (int) PixelUtil.toPixelFromDIP(1);
177+
178+
float ratio = (float) currentFontSize / (float) initialFontSize;
168179
ReactAbsoluteSizeSpan[] sizeSpans = text.getSpans(0, text.length(), ReactAbsoluteSizeSpan.class);
169180
for (ReactAbsoluteSizeSpan span : sizeSpans) {
170181
text.setSpan(
171-
new ReactAbsoluteSizeSpan(span.getSize() - 1),
182+
new ReactAbsoluteSizeSpan((int) Math.max((span.getSize() * ratio), minimumFontSize)),
172183
text.getSpanStart(span),
173184
text.getSpanEnd(span),
174185
text.getSpanFlags(span));

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class ReactTextView extends AppCompatTextView implements ReactCompoundVie
4848
private int mTextAlign = Gravity.NO_GRAVITY;
4949
private int mNumberOfLines = ViewDefaults.NUMBER_OF_LINES;
5050
private TextUtils.TruncateAt mEllipsizeLocation = TextUtils.TruncateAt.END;
51-
private boolean mAdjustsFontSizeToFit;
51+
private boolean mAdjustsFontSizeToFit = false;
5252
private int mLinkifyMaskType = 0;
5353
private boolean mNotifyOnInlineViewLayout;
5454

0 commit comments

Comments
 (0)