我一直试图给<TextInput>
添加字间距样式,但似乎只适用于<Text>
。在React Native中是否有一种方法可以增加<TextInput>
中字符之间的间距?
这会增加字符之间的空格。
<Text style={{'letterSpacing': 5}}>Some Text</Text>
这不会起作用
<TextInput style={{'letterSpacing': 5}} />
我一直试图给<TextInput>
添加字间距样式,但似乎只适用于<Text>
。在React Native中是否有一种方法可以增加<TextInput>
中字符之间的间距?
这会增加字符之间的空格。
<Text style={{'letterSpacing': 5}}>Some Text</Text>
这不会起作用
<TextInput style={{'letterSpacing': 5}} />
<TextInput letterSpacing={5} ... />
我在Stack Overflow上提问,为什么样式键也可以作为组件上的属性使用,但没有得到答案。
请注意,这不是在RN中设置组件样式的预期方法,因此不要将此解决方案视为永久性解决方案。
class Form extends React.Component {
constructor() {
super();
this.state = {
value: '',
};
this.onChangeText = this.onChangeText.bind(this);
}
onChangeText(e) {
const val = e.target.value.replace(/ /g, '').split('');
const spaced = val.join(' ');
this.setState({
value: spaced,
});
}
render() {
return(
<div style={{flexDirection: 'column'}}>
<div>Type Text</div>
<input
type='text'
onChange={this.onChangeText}
value={this.state.value}
/>
</div>
);
}
}
您应该在inputStyle属性中设置letterSpacing。
示例:
<TextInput inputStyle={{ letterSpacing: 20 }}/>
快来看看这个组件吧 https://github.com/wix/react-native-ui-lib#masked-input 它允许您以任何想要的方式呈现自定义掩码输入,而不会影响输入的实际值。
只需将文本放入textInput中,并像下面一样进行样式设置
<TextInput
placeholder={'1 2 3 4 5 6'}
style={textInputViewStyle}
onChangeText={setCode}
maxLength={6}
>
<Text style={{ letterSpacing: 19 }}>{code}</Text>
</TextInput>
onChangeText
来添加额外的空格。 - m_callens