Skip to content

Commit c7b499b

Browse files
Fix adjustsFontSizeToFit on iOS Fabric
1 parent 8ed0e1e commit c7b499b

8 files changed

Lines changed: 30 additions & 79 deletions

File tree

packages/react-native/ReactCommon/react/renderer/attributedstring/ParagraphAttributes.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ bool ParagraphAttributes::operator==(const ParagraphAttributes& rhs) const {
3131
rhs.includeFontPadding,
3232
rhs.android_hyphenationFrequency,
3333
rhs.textAlignVertical) &&
34-
floatEquality(minimumFontSize, rhs.minimumFontSize) &&
35-
floatEquality(maximumFontSize, rhs.maximumFontSize) &&
3634
floatEquality(minimumFontScale, rhs.minimumFontScale);
3735
}
3836

@@ -61,13 +59,9 @@ SharedDebugStringConvertibleList ParagraphAttributes::getDebugProps() const {
6159
adjustsFontSizeToFit,
6260
paragraphAttributes.adjustsFontSizeToFit),
6361
debugStringConvertibleItem(
64-
"minimumFontSize",
65-
minimumFontSize,
66-
paragraphAttributes.minimumFontSize),
67-
debugStringConvertibleItem(
68-
"maximumFontSize",
69-
maximumFontSize,
70-
paragraphAttributes.maximumFontSize),
62+
"minimumFontScale",
63+
minimumFontScale,
64+
paragraphAttributes.minimumFontScale),
7165
debugStringConvertibleItem(
7266
"includeFontPadding",
7367
includeFontPadding,

packages/react-native/ReactCommon/react/renderer/attributedstring/ParagraphAttributes.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,11 @@ class ParagraphAttributes : public DebugStringConvertible {
6363
*/
6464
HyphenationFrequency android_hyphenationFrequency{};
6565

66-
/*
67-
* In case of font size adjustment enabled, defines minimum and maximum
68-
* font sizes.
69-
*/
70-
Float minimumFontSize{std::numeric_limits<Float>::quiet_NaN()};
71-
Float maximumFontSize{std::numeric_limits<Float>::quiet_NaN()};
72-
7366
/*
7467
* Specifies the smallest possible scale a font can reach when
7568
* adjustsFontSizeToFit is enabled. (values 0.01-1.0).
7669
*/
77-
Float minimumFontScale{std::numeric_limits<Float>::quiet_NaN()};
70+
Float minimumFontScale{0.0};
7871

7972
/*
8073
* The vertical alignment of the text, causing the glyphs to be vertically
@@ -105,8 +98,7 @@ struct hash<facebook::react::ParagraphAttributes> {
10598
attributes.ellipsizeMode,
10699
attributes.textBreakStrategy,
107100
attributes.adjustsFontSizeToFit,
108-
attributes.minimumFontSize,
109-
attributes.maximumFontSize,
101+
attributes.minimumFontScale,
110102
attributes.includeFontPadding,
111103
attributes.android_hyphenationFrequency,
112104
attributes.minimumFontScale,

packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -952,18 +952,6 @@ inline ParagraphAttributes convertRawProp(
952952
"minimumFontScale",
953953
sourceParagraphAttributes.minimumFontScale,
954954
defaultParagraphAttributes.minimumFontScale);
955-
paragraphAttributes.minimumFontSize = convertRawProp(
956-
context,
957-
rawProps,
958-
"minimumFontSize",
959-
sourceParagraphAttributes.minimumFontSize,
960-
defaultParagraphAttributes.minimumFontSize);
961-
paragraphAttributes.maximumFontSize = convertRawProp(
962-
context,
963-
rawProps,
964-
"maximumFontSize",
965-
sourceParagraphAttributes.maximumFontSize,
966-
defaultParagraphAttributes.maximumFontSize);
967955
paragraphAttributes.includeFontPadding = convertRawProp(
968956
context,
969957
rawProps,
@@ -1062,8 +1050,7 @@ constexpr static MapBuffer::Key PA_KEY_TEXT_BREAK_STRATEGY = 2;
10621050
constexpr static MapBuffer::Key PA_KEY_ADJUST_FONT_SIZE_TO_FIT = 3;
10631051
constexpr static MapBuffer::Key PA_KEY_INCLUDE_FONT_PADDING = 4;
10641052
constexpr static MapBuffer::Key PA_KEY_HYPHENATION_FREQUENCY = 5;
1065-
constexpr static MapBuffer::Key PA_KEY_MINIMUM_FONT_SIZE = 6;
1066-
constexpr static MapBuffer::Key PA_KEY_MAXIMUM_FONT_SIZE = 7;
1053+
constexpr static MapBuffer::Key PA_KEY_MINIMUM_FONT_SCALE = 6;
10671054
constexpr static MapBuffer::Key PA_KEY_TEXT_ALIGN_VERTICAL = 8;
10681055

10691056
inline MapBuffer toMapBuffer(const ParagraphAttributes& paragraphAttributes) {
@@ -1088,9 +1075,7 @@ inline MapBuffer toMapBuffer(const ParagraphAttributes& paragraphAttributes) {
10881075
toString(*paragraphAttributes.textAlignVertical));
10891076
}
10901077
builder.putDouble(
1091-
PA_KEY_MINIMUM_FONT_SIZE, paragraphAttributes.minimumFontSize);
1092-
builder.putDouble(
1093-
PA_KEY_MAXIMUM_FONT_SIZE, paragraphAttributes.maximumFontSize);
1078+
PA_KEY_MINIMUM_FONT_SCALE, paragraphAttributes.minimumFontScale);
10941079

10951080
return builder.build();
10961081
}

packages/react-native/ReactCommon/react/renderer/components/text/BaseParagraphProps.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ void BaseParagraphProps::setProp(
102102
paragraphAttributes,
103103
minimumFontScale,
104104
"minimumFontScale");
105-
REBUILD_FIELD_SWITCH_CASE(
106-
paDefaults,
107-
value,
108-
paragraphAttributes,
109-
minimumFontSize,
110-
"minimumFontSize");
111-
REBUILD_FIELD_SWITCH_CASE(
112-
paDefaults,
113-
value,
114-
paragraphAttributes,
115-
maximumFontSize,
116-
"maximumFontSize");
117105
REBUILD_FIELD_SWITCH_CASE(
118106
paDefaults,
119107
value,

packages/react-native/ReactCommon/react/renderer/components/text/platform/android/react/renderer/components/text/HostPlatformParagraphProps.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,6 @@ folly::dynamic HostPlatformParagraphProps::getDiffProps(
132132
result["minimumFontScale"] = paragraphAttributes.minimumFontScale;
133133
}
134134

135-
if (!floatEquality(
136-
paragraphAttributes.minimumFontSize,
137-
oldProps->paragraphAttributes.minimumFontSize)) {
138-
result["minimumFontSize"] = paragraphAttributes.minimumFontSize;
139-
}
140-
141-
if (!floatEquality(
142-
paragraphAttributes.maximumFontSize,
143-
oldProps->paragraphAttributes.maximumFontSize)) {
144-
result["maximumFontSize"] = paragraphAttributes.maximumFontSize;
145-
}
146-
147135
if (paragraphAttributes.includeFontPadding !=
148136
oldProps->paragraphAttributes.includeFontPadding) {
149137
result["includeFontPadding"] = paragraphAttributes.includeFontPadding;

packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,8 @@ void BaseTextInputProps::setProp(
177177
paDefaults,
178178
value,
179179
paragraphAttributes,
180-
minimumFontSize,
181-
"minimumFontSize");
182-
REBUILD_FIELD_SWITCH_CASE(
183-
paDefaults,
184-
value,
185-
paragraphAttributes,
186-
maximumFontSize,
187-
"maximumFontSize");
180+
minimumFontScale,
181+
"minimumFontScale");
188182
REBUILD_FIELD_SWITCH_CASE(
189183
paDefaults,
190184
value,

packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,9 @@ folly::dynamic AndroidTextInputProps::getDiffProps(
394394
}
395395

396396
if (!floatEquality(
397-
paragraphAttributes.minimumFontSize,
398-
oldProps->paragraphAttributes.minimumFontSize)) {
399-
result["minimumFontSize"] = paragraphAttributes.minimumFontSize;
400-
}
401-
402-
if (!floatEquality(
403-
paragraphAttributes.maximumFontSize,
404-
oldProps->paragraphAttributes.maximumFontSize)) {
405-
result["maximumFontSize"] = paragraphAttributes.maximumFontSize;
397+
paragraphAttributes.minimumFontScale,
398+
oldProps->paragraphAttributes.minimumFontScale)) {
399+
result["minimumFontScale"] = paragraphAttributes.minimumFontScale;
406400
}
407401

408402
if (paragraphAttributes.includeFontPadding !=

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,22 @@ - (LinesMeasurements)getLinesForAttributedString:(facebook::react::AttributedStr
220220
return paragraphLines;
221221
}
222222

223+
- (CGFloat)_maximumFontSizeInAttributedString:(NSAttributedString *)attributedString
224+
{
225+
__block CGFloat maximumFontSize = 0.0;
226+
[attributedString enumerateAttribute:NSFontAttributeName
227+
inRange:NSMakeRange(0, attributedString.length)
228+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
229+
usingBlock:^(id _Nullable value, NSRange range, BOOL *_Nonnull stop) {
230+
CGFloat fontSize = ((UIFont *)value).pointSize;
231+
if (fontSize > maximumFontSize) {
232+
maximumFontSize = fontSize;
233+
}
234+
}];
235+
236+
return maximumFontSize;
237+
}
238+
223239
- (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttributedString *)attributedString
224240
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
225241
size:(CGSize)size
@@ -243,8 +259,8 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
243259
[textStorage addLayoutManager:layoutManager];
244260

245261
if (paragraphAttributes.adjustsFontSizeToFit) {
246-
CGFloat minimumFontSize = !isnan(paragraphAttributes.minimumFontSize) ? paragraphAttributes.minimumFontSize : 4.0;
247-
CGFloat maximumFontSize = !isnan(paragraphAttributes.maximumFontSize) ? paragraphAttributes.maximumFontSize : 96.0;
262+
CGFloat maximumFontSize = [self _maximumFontSizeInAttributedString:attributedString];
263+
CGFloat minimumFontSize = MAX(paragraphAttributes.minimumFontScale * maximumFontSize, 4.0);
248264
[textStorage scaleFontSizeToFitSize:size minimumFontSize:minimumFontSize maximumFontSize:maximumFontSize];
249265
}
250266

0 commit comments

Comments
 (0)