-->

TextInput not working as expected when programmati

2019-07-06 08:47发布

问题:

The below code does not work properly in iOS (iPhone 6). When I press ‘Push Me’, the word is not displayed completely and the cursor is also not shown. On further investigation, I found out that onChangeText and onContentSize change are not triggered. Pressing any key on the key board after pressing the ‘Push Me’ button displays the full word. I want the full word to be displayed when the button is clicked. It works, if I use value prop to update the TextInput. But I need to display formatted text like in the ‘TokenizedTextExample’ in this link: https://facebook.github.io/react-native/docs/textinput.html

import React, { Component } from 'react';
import {
  AppRegistry,
  TouchableOpacity,
  TextInput,
  Text,
  View
} from 'react-native';

export default class keypost extends Component {
  constructor(props) {
    super(props);
    this.state = {
      part: '',
      update: true
    };
  }


  myfunc() {
    this.setState({ part: <Text><Text style={{ color: 'blue' }}>"thivishnu_ks_is_good_boyo lllllll COurs uad is sfd_sdf llllsdf sfsddfadfadsfadsff dsafdfas"</Text> <Text>"fasdfa sjsjsjs jsjsjsj sjsjshshshsshshss"</Text></Text> })
  }
  render() {
    var parts = this.state.part
    return (
      <View style={{ paddingTop: 50 }}>
        <TextInput
          multiline={true}
          style={{ height: 300 }}
          >
          <Text>{parts}</Text>
        </TextInput>
        <TouchableOpacity style={{ height: 100, width: 100 }} onPress={this.myfunc.bind(this)} >
          <Text style={{}}>
            Push Me
            </Text>
        </TouchableOpacity>
      </View>
    );
  }
}

回答1:

Removing the fixed height given to TextInput will fix the initial hidden issue.

   <TextInput
     multiline={true}
   >
     <Text>{parts}</Text>
   </TextInput>