|
| 1 | +#import "FRNFontMetrics.h" |
| 2 | + |
| 3 | +static NSDictionary<NSString *, NSNumber *> *FRNRecognizedTextStyles() { |
| 4 | + static NSDictionary<NSString *, NSNumber *> *styles; |
| 5 | + static dispatch_once_t onceToken; |
| 6 | + dispatch_once(&onceToken, ^{ |
| 7 | + styles = @{ |
| 8 | + @"caption1": @(FRNTextStyleCaption1), |
| 9 | + @"caption2": @(FRNTextStyleCaption2), |
| 10 | + @"footnote": @(FRNTextStyleFootnote), |
| 11 | + @"subheadline": @(FRNTextStyleSubheadline), |
| 12 | + @"callout": @(FRNTextStyleCallout), |
| 13 | + @"body": @(FRNTextStyleBody), |
| 14 | + @"headline": @(FRNTextStyleHeadline), |
| 15 | + @"title3": @(FRNTextStyleTitle3), |
| 16 | + @"title2": @(FRNTextStyleTitle2), |
| 17 | + @"title1": @(FRNTextStyleTitle1), |
| 18 | + @"largeTitle": @(FRNTextStyleLargeTitle), |
| 19 | + }; |
| 20 | + }); |
| 21 | + return styles; |
| 22 | +} |
| 23 | + |
| 24 | +@implementation RCTConvert (FRNTextStyle) |
| 25 | + |
| 26 | +RCT_ENUM_CONVERTER(FRNTextStyle, FRNRecognizedTextStyles(), FRNTextStyleUndefined, integerValue) |
| 27 | + |
| 28 | +@end |
| 29 | + |
| 30 | +NS_ASSUME_NONNULL_BEGIN |
| 31 | + |
| 32 | +UIFontMetrics *FRNUIFontMetricsForTextStyle(FRNTextStyle textStyle) { |
| 33 | + static NSDictionary<NSNumber *, UIFontTextStyle> *mapping; |
| 34 | + static dispatch_once_t onceToken; |
| 35 | + dispatch_once(&onceToken, ^{ |
| 36 | + mapping = @{ |
| 37 | + @(FRNTextStyleCaption2): UIFontTextStyleCaption2, |
| 38 | + @(FRNTextStyleCaption1): UIFontTextStyleCaption1, |
| 39 | + @(FRNTextStyleFootnote): UIFontTextStyleFootnote, |
| 40 | + @(FRNTextStyleSubheadline): UIFontTextStyleSubheadline, |
| 41 | + @(FRNTextStyleCallout): UIFontTextStyleCallout, |
| 42 | + @(FRNTextStyleBody): UIFontTextStyleBody, |
| 43 | + @(FRNTextStyleHeadline): UIFontTextStyleHeadline, |
| 44 | + @(FRNTextStyleTitle3): UIFontTextStyleTitle3, |
| 45 | + @(FRNTextStyleTitle2): UIFontTextStyleTitle2, |
| 46 | + @(FRNTextStyleTitle1): UIFontTextStyleTitle1, |
| 47 | + @(FRNTextStyleLargeTitle): UIFontTextStyleLargeTitle, |
| 48 | + }; |
| 49 | + }); |
| 50 | + |
| 51 | + UIFontTextStyle uiFontTextStyle = mapping[@(textStyle)] ?: UIFontTextStyleBody; // Default to body if we don't recognize the specified ramp |
| 52 | + return [UIFontMetrics metricsForTextStyle:uiFontTextStyle]; |
| 53 | +} |
| 54 | + |
| 55 | +CGFloat FRNBaseSizeForTextStyle(FRNTextStyle textStyle) { |
| 56 | + static NSDictionary<NSNumber *, NSNumber *> *mapping; |
| 57 | + static dispatch_once_t onceToken; |
| 58 | + dispatch_once(&onceToken, ^{ |
| 59 | + // Values taken from https://developer.apple.com/design/human-interface-guidelines/foundations/typography/#specifications |
| 60 | + mapping = @{ |
| 61 | + @(FRNTextStyleCaption2): @11, |
| 62 | + @(FRNTextStyleCaption1): @12, |
| 63 | + @(FRNTextStyleFootnote): @13, |
| 64 | + @(FRNTextStyleSubheadline): @15, |
| 65 | + @(FRNTextStyleCallout): @16, |
| 66 | + @(FRNTextStyleBody): @17, |
| 67 | + @(FRNTextStyleHeadline): @17, |
| 68 | + @(FRNTextStyleTitle3): @20, |
| 69 | + @(FRNTextStyleTitle2): @22, |
| 70 | + @(FRNTextStyleTitle1): @28, |
| 71 | + @(FRNTextStyleLargeTitle): @34, |
| 72 | + }; |
| 73 | + }); |
| 74 | + |
| 75 | + NSNumber *baseSize = mapping[@(textStyle)] ?: @17; // Default to body size if we don't recognize the specified ramp |
| 76 | + return CGFLOAT_IS_DOUBLE ? [baseSize doubleValue] : [baseSize floatValue]; |
| 77 | +} |
| 78 | + |
| 79 | +@implementation FRNFontMetrics { |
| 80 | + BOOL _hasListeners; |
| 81 | +} |
| 82 | + |
| 83 | ++ (BOOL)requiresMainQueueSetup |
| 84 | +{ |
| 85 | + return YES; |
| 86 | +} |
| 87 | + |
| 88 | +RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(allScaleFactors) |
| 89 | +{ |
| 90 | + NSMutableDictionary *result = [NSMutableDictionary new]; |
| 91 | + [FRNRecognizedTextStyles() enumerateKeysAndObjectsUsingBlock:^(NSString * styleString, __unused NSNumber * boxedTextStyle, __unused BOOL * stop) { |
| 92 | + result[styleString] = [self scaleFactorForStyle:styleString]; |
| 93 | + }]; |
| 94 | + return result; |
| 95 | +} |
| 96 | + |
| 97 | +RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(scaleFactorForStyle:(NSString *)styleString) |
| 98 | +{ |
| 99 | + FRNTextStyle style = [RCTConvert FRNTextStyle:styleString]; |
| 100 | + UIFontMetrics *fontMetrics = FRNUIFontMetricsForTextStyle(style); |
| 101 | + CGFloat baseSize = FRNBaseSizeForTextStyle(style); |
| 102 | + CGFloat scaleFactor = [fontMetrics scaledValueForValue:baseSize] / baseSize; |
| 103 | + return @(scaleFactor); |
| 104 | +} |
| 105 | + |
| 106 | +#pragma mark - RCTEventEmitter |
| 107 | + |
| 108 | +- (NSArray<NSString *> *)supportedEvents |
| 109 | +{ |
| 110 | + return @[ @"onFontMetricsChanged" ]; |
| 111 | +} |
| 112 | + |
| 113 | +- (void)startObserving |
| 114 | +{ |
| 115 | + _hasListeners = YES; |
| 116 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 117 | + selector:@selector(onFontMetricsChanged:) |
| 118 | + name:UIContentSizeCategoryDidChangeNotification |
| 119 | + object:nil]; |
| 120 | +} |
| 121 | + |
| 122 | +- (void)stopObserving |
| 123 | +{ |
| 124 | + _hasListeners = NO; |
| 125 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 126 | +} |
| 127 | + |
| 128 | +#pragma mark - Event processing |
| 129 | + |
| 130 | +- (void)onFontMetricsChanged:(NSNotification *)notification { |
| 131 | + if (_hasListeners) { |
| 132 | + [self sendEventWithName:@"onFontMetricsChanged" body:@{@"newScaleFactors": [self allScaleFactors]}]; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +RCT_EXPORT_MODULE(); |
| 137 | + |
| 138 | +@end |
| 139 | + |
| 140 | +NS_ASSUME_NONNULL_END |
0 commit comments