-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Add Dynamic Type support for iOS (Paper and Fabric) #35017
Closed
Closed
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
61101b8
Non-Fabric dynamic text support
5483552
Dynamic type ramp: subhead -> subheadline
ddb70d2
RCTUIBaseSizeForDynamicTypeRamp -> RCTBaseSizeForDynamicTypeRamp
6dd3ead
Merge branch 'facebook-main' into core-ios-dynamic-type
544117f
Merge branch 'facebook-main' into core-ios-dynamic-type
6085f44
Fabric implementation of Dynamic Type
fc4ab43
More accurate scaling arithmetic
2393f04
RNTester: Dynamic Text -> Dynamic Type
355c7d2
Merge branch 'facebook-main' into core-ios-dynamic-type
0541f1a
Merge branch 'facebook-main' into core-ios-dynamic-type
98bd7b0
Add better links to Apple typography page
0254c3a
Add RCTDynamicTypeRamp.h to BUCK public headers
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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,73 @@ | ||
/* | ||
* 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/ | ||
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing this error on an internal build script:
RCTTextDecorationLineType
lives inReact/Views
. Wondering if this header should go there as well, vs if there is some internal bits we need to update.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what
RCTTextDecorationLineType
has to do with any of this. Considering that RCTDynamicTypeRamp.h is a newly created file with this PR, I'm guessing 0254c3a should help here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NickGerleman Can you confirm that this fix works?