|
| 1 | +import React, {Component, Fragment} from 'react'; |
| 2 | +import {ScrollView} from 'react-native'; |
| 3 | +import {View, Text, Button, DynamicFonts} from 'react-native-ui-lib'; |
| 4 | +import {renderMultipleSegmentOptions} from '../ExampleScreenPresenter'; |
| 5 | + |
| 6 | +enum FontLoadingEnum { |
| 7 | + SINGLE_FONT = 'singleFont', |
| 8 | + FONT_FAMILY = 'fontFamily' |
| 9 | +} |
| 10 | + |
| 11 | +type State = { |
| 12 | + fontLoadingType: FontLoadingEnum; |
| 13 | + loadedFonts: string[]; |
| 14 | +}; |
| 15 | + |
| 16 | +export default class DynamicFontsScreen extends Component<{}, State> { |
| 17 | + private fontDownloader: InstanceType<typeof DynamicFonts> = new DynamicFonts({debug: true}); |
| 18 | + |
| 19 | + state = { |
| 20 | + fontLoadingType: FontLoadingEnum.SINGLE_FONT, |
| 21 | + loadedFonts: [] |
| 22 | + }; |
| 23 | + |
| 24 | + renderSingleFont = () => { |
| 25 | + const {loadedFonts} = this.state; |
| 26 | + return ( |
| 27 | + <Fragment> |
| 28 | + <Text style={{fontSize: 24, lineHeight: 28, fontFamily: 'System'}}>{` |
| 29 | +System: |
| 30 | +ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz |
| 31 | + `}</Text> |
| 32 | + {loadedFonts.length > 0 && ( |
| 33 | + <Text marginT-20 style={{fontSize: 24, lineHeight: 28, fontFamily: loadedFonts[0]}}>{` |
| 34 | +${loadedFonts}: |
| 35 | +ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz |
| 36 | + `}</Text> |
| 37 | + )} |
| 38 | + <Button |
| 39 | + marginV-20 |
| 40 | + label="Load a single font" |
| 41 | + onPress={async () => { |
| 42 | + try { |
| 43 | + const loadedFonts = await this.fontDownloader.getFont({ |
| 44 | + fontUri: |
| 45 | + 'https://wixmp-1d257fba8470f1b562a0f5f2.wixmp.com/TypographyKits/fonts/Vollkorn/Vollkorn-Regular.ttf', |
| 46 | + fontName: 'Vollkorn-Regular', |
| 47 | + fontExtension: 'ttf' |
| 48 | + }); |
| 49 | + this.setState({loadedFonts: [loadedFonts]}); |
| 50 | + } catch (error) { |
| 51 | + console.log('Error!', error); |
| 52 | + } |
| 53 | + }} |
| 54 | + /> |
| 55 | + </Fragment> |
| 56 | + ); |
| 57 | + }; |
| 58 | + |
| 59 | + renderFontFamily = () => { |
| 60 | + const {loadedFonts} = this.state; |
| 61 | + return ( |
| 62 | + <Fragment> |
| 63 | + <Text style={{fontSize: 16, lineHeight: 18, fontFamily: 'System'}}>{` |
| 64 | +System: |
| 65 | +ABCDEFGH abcdefgh |
| 66 | + `}</Text> |
| 67 | + <ScrollView> |
| 68 | + {loadedFonts?.map(loadedFont => ( |
| 69 | + <Text key={loadedFont} style={{fontSize: 16, lineHeight: 18, fontFamily: loadedFont}}>{` |
| 70 | +${loadedFont}: |
| 71 | +ABCDEFGH abcdefgh |
| 72 | + `}</Text> |
| 73 | + ))} |
| 74 | + </ScrollView> |
| 75 | + |
| 76 | + <Button |
| 77 | + marginV-20 |
| 78 | + label="Load a complete font family" |
| 79 | + onPress={async () => { |
| 80 | + try { |
| 81 | + const loadedFonts = await this.fontDownloader.getFontFamily('https://wixmp-1d257fba8470f1b562a0f5f2.wixmp.com/TypographyKits/fonts/Vollkorn/', |
| 82 | + [ |
| 83 | + 'Bold', |
| 84 | + 'BoldItalic', |
| 85 | + 'ExtraBold', |
| 86 | + 'ExtraBoldItalic', |
| 87 | + 'Italic', |
| 88 | + 'Medium', |
| 89 | + 'MediumItalic', |
| 90 | + 'Regular', |
| 91 | + 'SemiBold' |
| 92 | + ], |
| 93 | + 'ttf', |
| 94 | + 'Vollkorn-'); |
| 95 | + this.setState({loadedFonts}); |
| 96 | + } catch (error) { |
| 97 | + console.log('Error!', error); |
| 98 | + } |
| 99 | + }} |
| 100 | + /> |
| 101 | + </Fragment> |
| 102 | + ); |
| 103 | + }; |
| 104 | + |
| 105 | + render() { |
| 106 | + const {fontLoadingType, loadedFonts} = this.state; |
| 107 | + return ( |
| 108 | + <View bg-grey80 flex padding-20> |
| 109 | + {renderMultipleSegmentOptions.call(this, 'Font loading:', 'fontLoadingType', [ |
| 110 | + {label: 'Single', value: FontLoadingEnum.SINGLE_FONT}, |
| 111 | + {label: 'Family', value: FontLoadingEnum.FONT_FAMILY} |
| 112 | + ])} |
| 113 | + <View flex center key={`${loadedFonts}`}> |
| 114 | + {fontLoadingType === FontLoadingEnum.SINGLE_FONT ? this.renderSingleFont() : this.renderFontFamily()} |
| 115 | + {loadedFonts && <Text text80>Loaded fonts: {loadedFonts.map(loadedFont => `${loadedFont} `)}</Text>} |
| 116 | + </View> |
| 117 | + </View> |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments