Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: Add a maxFontSizeMultiplier prop to <Text> and <TextInput> #20915

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type Props = $ReadOnly<{|
autoCorrect?: ?boolean,
autoFocus?: ?boolean,
allowFontScaling?: ?boolean,
maxContentSizeMultiplier?: ?boolean,
editable?: ?boolean,
keyboardType?: ?KeyboardType,
returnKeyType?: ?ReturnKeyType,
Expand Down Expand Up @@ -367,6 +368,14 @@ const TextInput = createReactClass({
* default is `true`.
*/
allowFontScaling: PropTypes.bool,
/**
* Specifies largest possible scale a font can reach when `allowFontScaling` is enabled.
* Possible values:
* `null/undefined` (default): inherit from the parent node or the global default (0)
* `0`: no max, ignore parent/global default
* `>= 1`: sets the maxContentSizeMultiplier of this node to this value
*/
maxContentSizeMultiplier: PropTypes.number,
/**
* If `false`, text is not editable. The default value is `true`.
*/
Expand Down Expand Up @@ -933,7 +942,11 @@ const TextInput = createReactClass({
);
if (childCount >= 1) {
children = (
<Text style={props.style} allowFontScaling={props.allowFontScaling}>
<Text
style={props.style}
allowFontScaling={props.allowFontScaling}
maxContentSizeMultiplier={props.maxContentSizeMultiplier}
>
{children}
</Text>
);
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/BaseText/RCTBaseTextViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ - (RCTShadowView *)shadowView
RCT_REMAP_SHADOW_PROPERTY(fontStyle, textAttributes.fontStyle, NSString)
RCT_REMAP_SHADOW_PROPERTY(fontVariant, textAttributes.fontVariant, NSArray)
RCT_REMAP_SHADOW_PROPERTY(allowFontScaling, textAttributes.allowFontScaling, BOOL)
RCT_REMAP_SHADOW_PROPERTY(maxContentSizeMultiplier, textAttributes.maxContentSizeMultiplier, CGFloat)
RCT_REMAP_SHADOW_PROPERTY(letterSpacing, textAttributes.letterSpacing, CGFloat)
// Paragraph Styles
RCT_REMAP_SHADOW_PROPERTY(lineHeight, textAttributes.lineHeight, CGFloat)
Expand Down
3 changes: 2 additions & 1 deletion Libraries/Text/RCTTextAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern NSString *const RCTTextAttributesTagAttributeName;
@property (nonatomic, copy, nullable) NSString *fontFamily;
@property (nonatomic, assign) CGFloat fontSize;
@property (nonatomic, assign) CGFloat fontSizeMultiplier;
@property (nonatomic, assign) CGFloat maxContentSizeMultiplier;
@property (nonatomic, copy, nullable) NSString *fontWeight;
@property (nonatomic, copy, nullable) NSString *fontStyle;
@property (nonatomic, copy, nullable) NSArray<NSString *> *fontVariant;
Expand Down Expand Up @@ -71,7 +72,7 @@ extern NSString *const RCTTextAttributesTagAttributeName;
- (UIFont *)effectiveFont;

/**
* Font size multiplier reflects `allowFontScaling` and `fontSizeMultiplier`.
* Font size multiplier reflects `allowFontScaling`, `fontSizeMultiplier`, and `maxContentSizeMultiplier`.
*/
- (CGFloat)effectiveFontSizeMultiplier;

Expand Down
16 changes: 15 additions & 1 deletion Libraries/Text/RCTTextAttributes.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
NSString *const RCTTextAttributesIsHighlightedAttributeName = @"RCTTextAttributesIsHighlightedAttributeName";
NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttributeName";

// Setting the default to 0 indicates that there is no max.
static CGFloat defaultMaxContentSizeMultiplier = 0.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would inline that.


@implementation RCTTextAttributes

- (instancetype)init
Expand All @@ -24,6 +27,7 @@ - (instancetype)init
_lineHeight = NAN;
_textDecorationStyle = NSUnderlineStyleSingle;
_fontSizeMultiplier = NAN;
_maxContentSizeMultiplier = NAN;
_alignment = NSTextAlignmentNatural;
_baseWritingDirection = NSWritingDirectionNatural;
_textShadowRadius = NAN;
Expand All @@ -49,6 +53,7 @@ - (void)applyTextAttributes:(RCTTextAttributes *)textAttributes
_fontFamily = textAttributes->_fontFamily ?: _fontFamily;
_fontSize = !isnan(textAttributes->_fontSize) ? textAttributes->_fontSize : _fontSize;
_fontSizeMultiplier = !isnan(textAttributes->_fontSizeMultiplier) ? textAttributes->_fontSizeMultiplier : _fontSizeMultiplier;
_maxContentSizeMultiplier = !isnan(textAttributes->_maxContentSizeMultiplier) ? textAttributes->_maxContentSizeMultiplier : _maxContentSizeMultiplier;
_fontWeight = textAttributes->_fontWeight ?: _fontWeight;
_fontStyle = textAttributes->_fontStyle ?: _fontStyle;
_fontVariant = textAttributes->_fontVariant ?: _fontVariant;
Expand Down Expand Up @@ -191,7 +196,15 @@ - (UIFont *)effectiveFont

- (CGFloat)effectiveFontSizeMultiplier
{
return !RCTHasFontHandlerSet() && _allowFontScaling && !isnan(_fontSizeMultiplier) ? _fontSizeMultiplier : 1.0;
bool fontScalingEnabled = !RCTHasFontHandlerSet() && _allowFontScaling;

if (fontScalingEnabled) {
CGFloat fontSizeMultiplier = !isnan(_fontSizeMultiplier) ? _fontSizeMultiplier : 1.0;
CGFloat maxContentSizeMultiplier = !isnan(_maxContentSizeMultiplier) ? _maxContentSizeMultiplier : defaultMaxContentSizeMultiplier;
return maxContentSizeMultiplier >= 1.0 ? fminf(maxContentSizeMultiplier, fontSizeMultiplier) : fontSizeMultiplier;
} else {
return 1.0;
}
}

- (UIColor *)effectiveForegroundColor
Expand Down Expand Up @@ -260,6 +273,7 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
RCTTextAttributesCompareObjects(_fontFamily) &&
RCTTextAttributesCompareFloats(_fontSize) &&
RCTTextAttributesCompareFloats(_fontSizeMultiplier) &&
RCTTextAttributesCompareFloats(_maxContentSizeMultiplier) &&
RCTTextAttributesCompareStrings(_fontWeight) &&
RCTTextAttributesCompareObjects(_fontStyle) &&
RCTTextAttributesCompareObjects(_fontVariant) &&
Expand Down
2 changes: 2 additions & 0 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const viewConfig = {
numberOfLines: true,
ellipsizeMode: true,
allowFontScaling: true,
maxContentSizeMultiplier: true,
disabled: true,
selectable: true,
selectionColor: true,
Expand Down Expand Up @@ -257,6 +258,7 @@ const RCTVirtualText =
validAttributes: {
...ReactNativeViewAttributes.UIView,
isHighlighted: true,
maxContentSizeMultiplier: true,
},
uiViewClassName: 'RCTVirtualText',
}));
Expand Down
1 change: 0 additions & 1 deletion Libraries/Text/Text/RCTTextViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ @interface RCTTextViewManager () <RCTUIManagerObserver>
@implementation RCTTextViewManager
{
NSHashTable<RCTTextShadowView *> *_shadowViews;
CGFloat _fontSizeMultiplier;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

RCT_EXPORT_MODULE(RCTText)
Expand Down
8 changes: 8 additions & 0 deletions Libraries/Text/TextPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ module.exports = {
* See https://facebook.github.io/react-native/docs/text.html#allowfontscaling
*/
allowFontScaling: PropTypes.bool,
/**
* Specifies largest possible scale a font can reach when `allowFontScaling` is enabled.
* Possible values:
* `null/undefined` (default): inherit from the parent node or the global default (0)
* `0`: no max, ignore parent/global default
* `>= 1`: sets the maxContentSizeMultiplier of this node to this value
*/
maxContentSizeMultiplier: PropTypes.number,
/**
* Indicates whether the view is an accessibility element.
*
Expand Down