-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dynamic Type support for iOS (Paper and Fabric) (#35017)
Summary: This adds Dynamic Type support in iOS as described [here](react-native-community/discussions-and-proposals#519). `Text` elements have a new prop, `dynamicTypeRamp`, that allows users to specify which font ramp a particular `Text` element should take on as the OS's accessibility setting for text size. The different types line up with different values of `UIFontTextStyle`. If not specified, we default to the current behavior. ~~For the moment, this change is only for Paper. I tried applying a corresponding change to Fabric by adding an additional field to [`facebook::react::TextAttributes`](https://github.com/facebook/react-native/blob/main/ReactCommon/react/renderer/attributedstring/TextAttributes.h) and changing [`RCTEffectiveFontSizeMultiplierFromTextAttributes`](https://github.com/facebook/react-native/blob/afb124dcf0cdf0db525acc7cfd2cea2742c64068/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm#L79-L84) to use that new field, but in the process I discovered that this function doesn't seem to ever get called, hence [this bug](#34990 ## Changelog [iOS] [Added] - Dynamic Type support Pull Request resolved: #35017 Test Plan: Validated with a test page in RNTester. Screenshots follow: A) Default text size B) Largest non-accessibility text size C) Largest accessibility text size, split across two screenshots due to size | A | B | C | |-|-|-| | ![Simulator Screen Shot - iPad (9th generation) - 2022-10-18 at 16 17 08](https://user-images.githubusercontent.com/717674/196562746-c8bbf53d-3c70-4e55-8600-0cfed8aacf5d.png) | ![Simulator Screen Shot - iPad (9th generation) - 2022-10-18 at 16 17 55](https://user-images.githubusercontent.com/717674/196563051-68bb0e34-c573-47ed-8c19-58ae45a7ce2b.png) | ![Simulator Screen Shot - iPad (9th generation) - 2022-10-18 at 16 18 33](https://user-images.githubusercontent.com/717674/196563185-61ede5ee-e79e-4af5-84a7-8f1e230a25f8.png) | ||| ![Simulator Screen Shot - iPad (9th generation) - 2022-10-18 at 16 18 42](https://user-images.githubusercontent.com/717674/196563208-2242efa2-5f24-466d-80f5-eb57a7678a67.png) | Reviewed By: sammy-SC Differential Revision: D40779346 Pulled By: NickGerleman fbshipit-source-id: efc7a8e9810a93afc82c5def97af15a2e8453d90
- Loading branch information
1 parent
5fda72f
commit 11c8bf3
Showing
18 changed files
with
405 additions
and
1,011 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import <React/RCTConvert.h> | ||
|
||
typedef NS_ENUM(NSInteger, RCTDynamicTypeRamp) { | ||
RCTDynamicTypeRampUndefined, | ||
RCTDynamicTypeRampCaption2, | ||
RCTDynamicTypeRampCaption1, | ||
RCTDynamicTypeRampFootnote, | ||
RCTDynamicTypeRampSubheadline, | ||
RCTDynamicTypeRampCallout, | ||
RCTDynamicTypeRampBody, | ||
RCTDynamicTypeRampHeadline, | ||
RCTDynamicTypeRampTitle3, | ||
RCTDynamicTypeRampTitle2, | ||
RCTDynamicTypeRampTitle1, | ||
RCTDynamicTypeRampLargeTitle | ||
}; | ||
|
||
@interface RCTConvert (DynamicTypeRamp) | ||
|
||
+ (RCTDynamicTypeRamp)RCTDynamicTypeRamp:(nullable id)json; | ||
|
||
@end | ||
|
||
/// Generates a `UIFontMetrics` instance representing a particular Dynamic Type ramp. | ||
UIFontMetrics *_Nonnull RCTUIFontMetricsForDynamicTypeRamp(RCTDynamicTypeRamp dynamicTypeRamp); | ||
/// The "reference" size for a particular font scale ramp, equal to a text element's size under default text size | ||
/// settings. | ||
CGFloat RCTBaseSizeForDynamicTypeRamp(RCTDynamicTypeRamp dynamicTypeRamp); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <React/RCTDynamicTypeRamp.h> | ||
|
||
@implementation RCTConvert (DynamicTypeRamp) | ||
|
||
RCT_ENUM_CONVERTER( | ||
RCTDynamicTypeRamp, | ||
(@{ | ||
@"caption2" : @(RCTDynamicTypeRampCaption2), | ||
@"caption1" : @(RCTDynamicTypeRampCaption1), | ||
@"footnote" : @(RCTDynamicTypeRampFootnote), | ||
@"subheadline" : @(RCTDynamicTypeRampSubheadline), | ||
@"callout" : @(RCTDynamicTypeRampCallout), | ||
@"body" : @(RCTDynamicTypeRampBody), | ||
@"headline" : @(RCTDynamicTypeRampHeadline), | ||
@"title3" : @(RCTDynamicTypeRampTitle3), | ||
@"title2" : @(RCTDynamicTypeRampTitle2), | ||
@"title1" : @(RCTDynamicTypeRampTitle1), | ||
@"largeTitle" : @(RCTDynamicTypeRampLargeTitle), | ||
}), | ||
RCTDynamicTypeRampUndefined, | ||
integerValue) | ||
|
||
@end | ||
|
||
UIFontMetrics *RCTUIFontMetricsForDynamicTypeRamp(RCTDynamicTypeRamp dynamicTypeRamp) | ||
{ | ||
static NSDictionary<NSNumber *, UIFontTextStyle> *mapping; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
mapping = @{ | ||
@(RCTDynamicTypeRampCaption2) : UIFontTextStyleCaption2, | ||
@(RCTDynamicTypeRampCaption1) : UIFontTextStyleCaption1, | ||
@(RCTDynamicTypeRampFootnote) : UIFontTextStyleFootnote, | ||
@(RCTDynamicTypeRampSubheadline) : UIFontTextStyleSubheadline, | ||
@(RCTDynamicTypeRampCallout) : UIFontTextStyleCallout, | ||
@(RCTDynamicTypeRampBody) : UIFontTextStyleBody, | ||
@(RCTDynamicTypeRampHeadline) : UIFontTextStyleHeadline, | ||
@(RCTDynamicTypeRampTitle3) : UIFontTextStyleTitle3, | ||
@(RCTDynamicTypeRampTitle2) : UIFontTextStyleTitle2, | ||
@(RCTDynamicTypeRampTitle1) : UIFontTextStyleTitle1, | ||
@(RCTDynamicTypeRampLargeTitle) : UIFontTextStyleLargeTitle, | ||
}; | ||
}); | ||
|
||
id textStyle = | ||
mapping[@(dynamicTypeRamp)] ?: UIFontTextStyleBody; // Default to body if we don't recognize the specified ramp | ||
return [UIFontMetrics metricsForTextStyle:textStyle]; | ||
} | ||
|
||
CGFloat RCTBaseSizeForDynamicTypeRamp(RCTDynamicTypeRamp dynamicTypeRamp) | ||
{ | ||
static NSDictionary<NSNumber *, NSNumber *> *mapping; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
// Values taken from | ||
// https://developer.apple.com/design/human-interface-guidelines/foundations/typography/#specifications | ||
mapping = @{ | ||
@(RCTDynamicTypeRampCaption2) : @11, | ||
@(RCTDynamicTypeRampCaption1) : @12, | ||
@(RCTDynamicTypeRampFootnote) : @13, | ||
@(RCTDynamicTypeRampSubheadline) : @15, | ||
@(RCTDynamicTypeRampCallout) : @16, | ||
@(RCTDynamicTypeRampBody) : @17, | ||
@(RCTDynamicTypeRampHeadline) : @17, | ||
@(RCTDynamicTypeRampTitle3) : @20, | ||
@(RCTDynamicTypeRampTitle2) : @22, | ||
@(RCTDynamicTypeRampTitle1) : @28, | ||
@(RCTDynamicTypeRampLargeTitle) : @34, | ||
}; | ||
}); | ||
|
||
NSNumber *baseSize = | ||
mapping[@(dynamicTypeRamp)] ?: @17; // Default to body size if we don't recognize the specified ramp | ||
return CGFLOAT_IS_DOUBLE ? [baseSize doubleValue] : [baseSize floatValue]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.