Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

react native - dynamic fontFamily in FontList Item

I'm using the FlatList to show all the default fonts available on Android for React Native. But I want to change the font family dynamically to actually look like that font. This will get a lot more interesting on iOS that has over 100 or when I start adding custom fonts.. but to start I just went with the 10 Android fonts that default out of the box. I can hard code it and they all look the same, but when I feed it the variable I called fontJB it doesn't even pick it up. I tried wrapping it in a separate bracket like the title but that breaks the whole page and it doesn't even render..

monospace roboto normal

const DATA = [
  {
    id: '01',
    title: 'normal',
    fontJB: 'normal',
  },
  {
    id: '02',
    title: 'notoserif',
    fontJB: 'notoserif',
  },
  {
    id: '03',
    title: 'sans-serif',
    fontJB: 'sans-serif',
  },
  {
    id: '04',
    title: 'sans-serif-light',
    fontJB: 'sans-serif-light',
  },
  {
    id: '05',
    title: 'sans-serif-thin',
    fontJB: 'sans-serif-thin',
  },
  {
    id: '06',
    title: 'sans-serif-condensed',
    fontJB: 'sans-serif-condensed',
  },
  {
    id: '07',
    title: 'sans-serif-medium',
    fontJB: 'sans-serif-medium',
  },
  {
    id: '08',
    title: 'serif',
    fontJB: 'serif',
  },
  {
    id: '09',
    title: 'Roboto',
    fontJB: 'Roboto',
  },
  {
    id: '10',
    title: 'monospace',
    fontJB: 'monospace',
  },
];

const Item = ({ title, fontJB }) => (
  <View style={styles.item}>
    <Text style={{ fontSize: 20, fontFamily: 'Roboto' }}>{title}</Text>
  </View>
);

const FontList = () => {
  const renderItem = ({ item }) => (
    <Item title={item.title} />
  );

fontFamily

fontSize

  {
    id: '09',
    title: 'Roboto',
    fontJB: 'Roboto',
  },
  {
    id: '10',
    title: 'monospace',
    fontJB: 'monospace',
  },
];

const Item = ({ title, fontJB }) => (
  <View style={styles.item}>
    <Text style={{ fontSize: 20, fontFamily: fontJB }}>{title}</Text>
  </View>
);
question from:https://stackoverflow.com/questions/65926107/dynamic-fontfamily-in-fontlist-item

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While setting it in the Item, I was failing to pass it into the FontList, it's working now.

const Item = ({ title, fontJB }) => (
  <View style={styles.item}>
    <Text style={{ fontSize: 20, fontFamily: fontJB }}>{title}</Text>
  </View>
);

const FontList = () => {
  const renderItem = ({ item }) => (
    <Item title={item.title} fontJB={item.fontJB} />
  );

  return (
    <SafeAreaView style={styles.container}>
      <FlatList
        data={DATA}
        renderItem={renderItem}
        keyExtractor={item => item.id}
      />
    </SafeAreaView>
  );
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...