React Native add bold or italics to single words in field

后端 未结 12 2554
攒了一身酷
攒了一身酷 2020-12-13 05:14

How do I make a single word in a Text field bold or italics? Kind of like this:

This is a sentence with one word in bold

        
相关标签:
12条回答
  • 2020-12-13 05:43

    You can use <Text> like a container for your other text components. This is example:

    ...
    <Text>
      <Text>This is a sentence</Text>
      <Text style={{fontWeight: "bold"}}> with</Text>
      <Text> one word in bold</Text>
    </Text>
    ...
    

    Here is an example.

    0 讨论(0)
  • 2020-12-13 05:44

    I am a maintainer of react-native-spannable-string

    Nested <Text/> component with custom style works well but maintainability is low.

    I suggest you build spannable string like this with this library.

    SpannableBuilder.getInstance({ fontSize: 24 })
        .append('Using ')
        .appendItalic('Italic')
        .append(' in Text')
        .build()
    
    0 讨论(0)
  • 2020-12-13 05:46

    for example!

    const TextBold = (props) => <Text style={{fontWeight: 'bold'}}>Text bold</Text>
    

    <Text> 123<TextBold/> </Text>

    0 讨论(0)
  • 2020-12-13 05:50

    Use this react native library

    To install

    npm install react-native-htmlview --save

    Basic Usage

     import React from 'react';
     import HTMLView from 'react-native-htmlview';
    
      class App extends React.Component {
      render() {
       const htmlContent = 'This is a sentence <b>with</b> one word in bold';
    
      return (
       <HTMLView
         value={htmlContent}
       />    );
      }
    }
    

    Supports almost all html tags.

    For more advanced usage like

    1. Link handling
    2. Custom Element Rendering

    View this ReadMe

    0 讨论(0)
  • 2020-12-13 05:51

    For a more web-like feel:

    const B = (props) => <Text style={{fontWeight: 'bold'}}>{props.children}</Text>
    
    <Text>I am in <B>bold</B> yo.</Text>
    
    0 讨论(0)
  • 2020-12-13 05:58

    You can also put a Text tag inside of another Text tag. The second text tag will inherit the styling of the first, but you maintain the ability to style it independently from its parent.

    <Text style={styles.bold}>Level: 
        <Text style={styles.normal}>Easy</Text>
    </Text>
    
    //in your stylesheet...
    
      bold: {
        fontSize: 25,
        fontWeight: "bold",
        color: "blue",
      },
      normal: {
      // will inherit size and color attributes
        fontWeight: "normal",
      }
    
    
    0 讨论(0)
提交回复
热议问题