首页 > 解决方案 > React-Native 在 TextInput 中一起调整表情符号和文本大小

问题描述

我想在 TextInput 中使用表情符号调整文本大小,只有文本效果很好,但插入表情符号时不行。

const [fontSize, setFontSize] = useState(16);
const input = useRef(null);

// resize Input func
const onFontSizeChange = () => {
    setFontSize(fontSize + 5);

    input.current.setNativeProps({
      style: {
        fontSize: fontSize + 5, 
      },
    });

}

<TextInput 
    ref={input}
    multiline={true} 
    style={{fontSize: 16}}
    forceStrutHeight={true}  
    value={textValue}
    onChangeText={typedText => {
        validate(typedText);
    }}
/>

我该怎么做?

标签: react-nativeemojitextinput

解决方案


只需使用状态对象作为样式并在单击按钮时更改样式对象。

const [style, setStyle] = useState({ fontSize: 16})

const onButtonClick = () => {
    setStyle({ fontSize: 20, lineHeight: 23 })
}

<TextInput 
    multiline={true} 
    style={style}
    forceStrutHeight={true}  
    value={textValue}
    onChangeText={typedText => {
        validate(typedText);
    }}
/>

推荐阅读