Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native nested text with differnt fontsizes (initial, first letter)

I currently write a news reader app in react-native and want to style the first letter of an article as an initial, like below:

An example of an initial

For my first attempt I use the nested text aproach. The code is attached below. And this is my current result:

Result with react-native

The code in the render function:

<View style={styles.container}>
    <Text style={styles.text}>
        <Text style={styles.initial}>W</Text>
        <Text>
            elcome to React Native! To get started, edit index.android.js To get started, edit index.android.js To get started, edit index.android.js
        </Text>
    </Text>
</View>

My stylesheet:

const fontSize = 14;

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
    },
    initial: {
        fontSize: fontSize * 2,            
    },
    text: {
        fontSize: fontSize,
        color: '#333333',
    },
});

My Question: How can I style my first char to get a pretty initial?

Environment

  • react: 16.0.0-alpha.6
  • react-native: 0.44.2
  • Android 6.0 on an emulated Nexus 5
like image 908
Daniel Lang Avatar asked May 30 '17 09:05

Daniel Lang


2 Answers

you can try in this way too, add the regular style for parent Text element and required style for the "W" text child element.

export default class DemoProject extends Component {
  render() {
    return (
      <View style={styles.container}>
        *<Text>
          <Text style={[styles.textCommon, styles.firstLetter]}>W</Text>
        elcome</Text>*
      </View>
    );
  }
}


  text: {
        fontSize: fontSize,
        color: '#333333',
    }
like image 109
Manjunath Avatar answered Oct 19 '22 12:10

Manjunath


This is one way of doing it:

export default class DemoProject extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={[styles.textCommon, styles.firstLetter]}>W</Text>
        <Text>elcome</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    flexDirection: 'row'
  },
  textCommon: {
    color: '#333333',
  },
  firstLetter: {
    fontSize: 23,
  }
});
like image 25
Ismailp Avatar answered Oct 19 '22 10:10

Ismailp