Remove vertical spacing from React Native text
P粉211600174
P粉211600174 2023-09-02 09:04:40
0
1
526
<p>I made a CustomText component to use the Poppins font family. But when using it, I get vertical space that ends up getting bigger as the fontsize increases, creating unnecessary space between the two CustomText components. </p> <p>I tried using padding:0,margin:0,line-height:undefined | fontSize but none of them work. This is my CustomText code: </p> <pre class="brush:js;toolbar:false;">const CustomText: React.FC<CustomTextProps> = ({ style, isBold, isItalic, fontSize, color, ...restProps }) => { fontSize ??= fontSizes.xsm; const combinedStyles = [ styles.defaultText, style, isBold && styles.boldText, isItalic && styles.italicText, { fontSize: fontSize }, { color: color ?? "black" }, ]; return <Text style={combinedStyles} {...restProps} />; }; const styles = StyleSheet.create({ defaultText: { fontFamily: "Poppins-Regular", }, boldText: { fontFamily: "Poppins-Bold", }, italicText: { fontFamily: "Poppins-Italic", }, }); </pre> <p>Here is an example of what happens when I use the sample image</p> <p>I used backgroundColor:'green' to see the vertical space and it was too much. </p> <p>I want to control the spacing between text elements. I'm new to react native, any help would be greatly appreciated. Thank you</p>
P粉211600174
P粉211600174

reply all(1)
P粉514001887

By default, the lineHeight property is set to a multiple of the font size.

You can set it yourself by adding {lineHeight: fontSize} (or any other desired amount) to combinedStyles.

const combinedStyles = [
    styles.defaultText,
    style,
    isBold && styles.boldText,
    isItalic && styles.italicText,
    { fontSize: fontSize },
    { color: color ?? "black" },
    { lineHeight: fontSize },
  ];

Also try setting paddingTop or paddingBottom or mar​​rginTop or mar​​rginBottom individually, as they may be Overrides general padding and margins defined elsewhere.

Also set includeFontPadding: false, because there may be default font padding.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template