Rich ReactNative TextInput

时间:2016-06-17 20:24:40

标签: react-native

任何方式来创造一个富裕的" React Native中的TextInput?也许不是一个完整的wysiwyg,但也许只是改变各种文字的文字颜色;比如Twitter或Facebook上的@mention功能。

4 个答案:

答案 0 :(得分:8)

解决方案是,您可以<Text>使用<TextInput>元素作为子项:

<TextInput>
    whoa no way <Text style={{color:'red'}}>rawr</Text>
</TextInput>

答案 1 :(得分:3)

查看react-native文档中的TokenizedTextExample。我认为这会让你接近你想要做的事情。相关代码如下:

class TokenizedTextExample extends React.Component {
  state: any;

  constructor(props) {
    super(props);
    this.state = {text: 'Hello #World'};
  }
  render() {

    //define delimiter
    let delimiter = /\s+/;

    //split string
    let _text = this.state.text;
    let token, index, parts = [];
    while (_text) {
      delimiter.lastIndex = 0;
      token = delimiter.exec(_text);
      if (token === null) {
        break;
      }
      index = token.index;
      if (token[0].length === 0) {
        index = 1;
      }
      parts.push(_text.substr(0, index));
      parts.push(token[0]);
      index = index + token[0].length;
      _text = _text.slice(index);
    }
    parts.push(_text);

    //highlight hashtags
    parts = parts.map((text) => {
      if (/^#/.test(text)) {
        return <Text key={text} style={styles.hashtag}>{text}</Text>;
      } else {
        return text;
      }
    });

    return (
      <View>
        <TextInput
          multiline={true}
          style={styles.multiline}
          onChangeText={(text) => {
            this.setState({text});
          }}>
          <Text>{parts}</Text>
        </TextInput>
      </View>
    );
  }
}

答案 2 :(得分:2)

您必须使用正则表达式才能实现此行为。有人已经为此创建了包,请查看react-native-parsed-text

此库允许您使用RegExp或预定义模式解析文本并提取部件。目前有3种预定义类型:网址,电话和电子邮件。

来自他们的github的例子

<ParsedText
          style={styles.text}
          parse={
            [
              {type: 'url',                       style: styles.url, onPress: this.handleUrlPress},
              {type: 'phone',                     style: styles.phone, onPress: this.handlePhonePress},
              {type: 'email',                     style: styles.email, onPress: this.handleEmailPress},
              {pattern: /Bob|David/,              style: styles.name, onPress: this.handleNamePress},
              {pattern: /\[(@[^:]+):([^\]]+)\]/i, style: styles.username, onPress: this.handleNamePress, renderText: this.renderText},
              {pattern: /42/,                     style: styles.magicNumber},
              {pattern: /#(\w+)/,                 style: styles.hashTag},
            ]
          }
        >
          Hello this is an example of the ParsedText, links like http://www.google.com or http://www.facebook.com are clickable and phone number 444-555-6666 can call too.
          But you can also do more with this package, for example Bob will change style and David too. foo@gmail.com
          And the magic number is 42!
          #react #react-native
</ParsedText>

答案 3 :(得分:0)

这个问题是在不久前问的,但是我认为我的回答可以帮助其他人寻找如何为字符串的@mention部分上色。我不确定我做的方法是干净还是“反应”的方法,但是这是我做的方法: 我将输入的字符串分割成一个空格作为分隔符。然后,我遍历数组,如果当前项与@ mention / @ user的模式匹配,则将其替换为Text标记并应用样式;否则退货。最后,我在TextInput元素内部渲染了inputText数组(包含字符串和jsx元素)。希望这可以帮助!

render() {
let inputText = this.state.content;
if (inputText){
  inputText = inputText.split(/(\s)/g).map((item, i) => {
    if (/@[a-zA-Z0-9]+/g.test(item)){
      return <Text key={i} style={{color: 'green'}}>{item}</Text>;
    } 
    return item;
  })
return <TextInput>{inputText}</TextInput>

Result

相关问题