Last active
January 20, 2021 15:23
-
-
Save crylico/d24ee57750fadc7e5f95 to your computer and use it in GitHub Desktop.
DynamicType in React Native
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
#import "RCTBridgeModule.h" | |
@interface DynamicType : NSObject <RCTBridgeModule> | |
@end |
This file contains hidden or 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
#import "DynamicType.h" | |
#import <UIKit/UIKit.h> | |
@implementation DynamicType | |
RCT_EXPORT_MODULE(); | |
- (NSDictionary *)constantsToExport { | |
NSDictionary *dynamicSizes = @{ @"Headline" : @([UIFont preferredFontForTextStyle:UIFontTextStyleHeadline].fontDescriptor.pointSize), | |
@"Subheadline" : @([UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline].fontDescriptor.pointSize), | |
@"Body" : @([UIFont preferredFontForTextStyle:UIFontTextStyleBody].fontDescriptor.pointSize), | |
@"Caption1" : @([UIFont preferredFontForTextStyle:UIFontTextStyleCaption1].fontDescriptor.pointSize), | |
@"Caption2" : @([UIFont preferredFontForTextStyle:UIFontTextStyleCaption2].fontDescriptor.pointSize), | |
@"Footnote" : @([UIFont preferredFontForTextStyle:UIFontTextStyleFootnote].fontDescriptor.pointSize)}; | |
return dynamicSizes; | |
} | |
@end |
This file contains hidden or 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
/* @flow */ | |
'use strict'; | |
var React = require('react-native'); | |
var DynamicType = require('NativeModules').DynamicType; | |
var { | |
StyleSheet, | |
View, | |
Text | |
} = React; | |
var SampleComponent = React.createClass({ | |
render: function() { | |
return ( | |
<View style={styles.container}> | |
<Text style={[styles.message, {fontSize: DynamicType.Headline}]}>Headline</Text> | |
<Text style={[styles.message, {fontSize: DynamicType.Subheadline}]}>Sub</Text> | |
<Text style={[styles.message, {fontSize: DynamicType.Body}]}>Body</Text> | |
<Text style={[styles.message, {fontSize: DynamicType.Caption1}]}>Cap1</Text> | |
<Text style={[styles.message, {fontSize: DynamicType.Caption2}]}>Cap2</Text> | |
<Text style={[styles.message, {fontSize: DynamicType.Footnote}]}>Footnote</Text> | |
</View> | |
); | |
} | |
}); | |
var styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: 'red', | |
justifyContent: 'center' | |
}, | |
message: { | |
textAlign: 'center' | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment