|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +// TODO(macOS GH#774) |
| 9 | + |
| 10 | +#import <React/RCTDynamicTypeRamp.h> |
| 11 | + |
| 12 | +@implementation RCTConvert (DynamicTypeRamp) |
| 13 | + |
| 14 | +RCT_ENUM_CONVERTER(RCTDynamicTypeRamp, (@{ |
| 15 | + @"caption2": @(RCTDynamicTypeRampCaption2), |
| 16 | + @"caption1": @(RCTDynamicTypeRampCaption1), |
| 17 | + @"footnote": @(RCTDynamicTypeRampFootnote), |
| 18 | + @"subhead": @(RCTDynamicTypeRampSubhead), |
| 19 | + @"callout": @(RCTDynamicTypeRampCallout), |
| 20 | + @"body": @(RCTDynamicTypeRampBody), |
| 21 | + @"headline": @(RCTDynamicTypeRampHeadline), |
| 22 | + @"title3": @(RCTDynamicTypeRampTitle3), |
| 23 | + @"title2": @(RCTDynamicTypeRampTitle2), |
| 24 | + @"title1": @(RCTDynamicTypeRampTitle1), |
| 25 | + @"largeTitle": @(RCTDynamicTypeRampLargeTitle), |
| 26 | +}), RCTDynamicTypeRampUndefined, integerValue) |
| 27 | + |
| 28 | +@end |
| 29 | + |
| 30 | +#if !TARGET_OS_OSX // [TODO(macOS GH#774) |
| 31 | +UIFontMetrics *RCTUIFontMetricsForDynamicTypeRamp(RCTDynamicTypeRamp dynamicTypeRamp) { |
| 32 | + static NSDictionary<NSNumber *, UIFontTextStyle> *mapping; |
| 33 | + static dispatch_once_t onceToken; |
| 34 | + dispatch_once(&onceToken, ^{ |
| 35 | + mapping = @{ |
| 36 | + @(RCTDynamicTypeRampCaption2): UIFontTextStyleCaption2, |
| 37 | + @(RCTDynamicTypeRampCaption1): UIFontTextStyleCaption1, |
| 38 | + @(RCTDynamicTypeRampFootnote): UIFontTextStyleFootnote, |
| 39 | + @(RCTDynamicTypeRampSubhead): UIFontTextStyleSubheadline, |
| 40 | + @(RCTDynamicTypeRampCallout): UIFontTextStyleCallout, |
| 41 | + @(RCTDynamicTypeRampBody): UIFontTextStyleBody, |
| 42 | + @(RCTDynamicTypeRampHeadline): UIFontTextStyleHeadline, |
| 43 | + @(RCTDynamicTypeRampTitle3): UIFontTextStyleTitle3, |
| 44 | + @(RCTDynamicTypeRampTitle2): UIFontTextStyleTitle2, |
| 45 | + @(RCTDynamicTypeRampTitle1): UIFontTextStyleTitle1, |
| 46 | + @(RCTDynamicTypeRampLargeTitle): UIFontTextStyleLargeTitle, |
| 47 | + }; |
| 48 | + }); |
| 49 | + |
| 50 | + id textStyle = mapping[@(dynamicTypeRamp)] ?: UIFontTextStyleBody; // Default to body if we don't recognize the specified ramp |
| 51 | + return [UIFontMetrics metricsForTextStyle:textStyle]; |
| 52 | +} |
| 53 | + |
| 54 | +CGFloat RCTUIBaseSizeForDynamicTypeRamp(RCTDynamicTypeRamp dynamicTypeRamp) { |
| 55 | + static NSDictionary<NSNumber *, NSNumber *> *mapping; |
| 56 | + static dispatch_once_t onceToken; |
| 57 | + dispatch_once(&onceToken, ^{ |
| 58 | + // Values taken from https://developer.apple.com/design/human-interface-guidelines/foundations/typography/ |
| 59 | + mapping = @{ |
| 60 | + @(RCTDynamicTypeRampCaption2): @11, |
| 61 | + @(RCTDynamicTypeRampCaption1): @12, |
| 62 | + @(RCTDynamicTypeRampFootnote): @13, |
| 63 | + @(RCTDynamicTypeRampSubhead): @15, |
| 64 | + @(RCTDynamicTypeRampCallout): @16, |
| 65 | + @(RCTDynamicTypeRampBody): @17, |
| 66 | + @(RCTDynamicTypeRampHeadline): @17, |
| 67 | + @(RCTDynamicTypeRampTitle3): @20, |
| 68 | + @(RCTDynamicTypeRampTitle2): @22, |
| 69 | + @(RCTDynamicTypeRampTitle1): @28, |
| 70 | + @(RCTDynamicTypeRampLargeTitle): @34, |
| 71 | + }; |
| 72 | + }); |
| 73 | + |
| 74 | + NSNumber *baseSize = mapping[@(dynamicTypeRamp)] ?: @17; // Default to body size if we don't recognize the specified ramp |
| 75 | + return CGFLOAT_IS_DOUBLE ? [baseSize doubleValue] : [baseSize floatValue]; |
| 76 | +} |
| 77 | +#endif // ]TODO(macOS GH#774) |
0 commit comments