React Native 富文本封装

import React, {Component} from 'react';
import {
    StyleSheet,
    View,
    Dimensions,
    TouchableOpacity,
    TextInput,
    Platform,
    Text
} from 'react-native';
const {width, height} = Dimensions.get('window');

//模块声名并导出
export default class TextInputView extends React.Component {

    //属性声名
    static propTypes = {
        style:React.PropTypes.any,
        inputStyle:React.PropTypes.any,
        maxLength:React.PropTypes.any, // 限制文字长度
        placeholder:React.PropTypes.string,  // 占位文字
        minHeight:React.PropTypes.any,   // 最小高度
        showCount:React.PropTypes.bool


    };
    //默认属性
    static defaultProps = {
        maxLength: 100,
        showCount: true,
        minHeight: defaultMinHeight
    };
    //构造函数
    constructor(props) {
        super(props);
        //状态机变量声明
        this.state = {
            text: '',
        };
    }

    //渲染
    //渲染
    render() {
        return (
            <View style={styles.container}>
                <View style={[styles.inputViewStyle,this.props.style,{minHeight:this.props.minHeight}]}>
                    <TextInput
                        style={[styles.inputTextStyle,this.props.inputStyle,{minHeight:this.props.minHeight}]}
                        placeholder={this.props.placeholder ? this.props.placeholder :'请输入'}
                        multiline={true}
                        paddingVertical={0}
                        selectionColor = {'gray'}
                        textAlignVertical={'top'}
                        placeholderTextColor={'#b2b2b2'}
                        underlineColorAndroid={'transparent'}
                        maxLength={this.props.maxLength}
                        defaultValue = {this.state.text}
                        onChangeText={
                                (text) => {
                                    this.setState({
                                        text: text
                                    })
                                }
                        }
                    />
                    {
                        this.props.showCount ?
                            <Text style={{position: 'absolute', bottom: 5, right: 10, fontSize: 14}}>
                                {this.state.text.length}/{this.props.maxLength}
                            </Text>
                            :
                            null
                    }
                </View>
            </View>
        );
    }
};

const defaultMinHeight = 100

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        backgroundColor: '#FFFFFF',
    },
    inputViewStyle: {
        width: width - 20,
        minHeight: defaultMinHeight,

        borderWidth: 1,
        borderColor: '#ddd8d8',
        borderRadius: 4,

    },
    inputTextStyle: {
        fontSize: 14,
        color: '#666666',
        width: '100%',
        minHeight: defaultMinHeight,
        padding: 10,
        paddingBottom: 30,
        paddingTop: 10
    }
});

使用:

<TextInputView
      style={{marginTop: 64,marginBottom: 10}}
      placeholder="请您在此描述问题"
      minHeight={100} // 最小高度,默认为100
      maxLength={500} // 最大长度,默认为100
      onChangeText={(text)=>{this.setState({remarkText: text});}}
      showCount={true} // 展示剩余文字, 默认为true
/>

效果如图:

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
支持拖拽 复制 截图 excel ----------------------------------------------------------------------------------------------------------------------------------------------复制代码 /** * Created by zhanglei on 2017/5/23. */ import React, { Component, PropTypes } from 'react'; import { Icon,Modal,message } from 'antd'; import ContentEditable from 'react-contenteditable' import './edit.less' export default class Editor extends Component { static propTypes = { className: PropTypes.string, value:PropTypes.string, editColor:PropTypes.string, }; constructor(props){ super(props); ['inputTextChange','onchangefile','onpaste','ondrop','onParseOrDrop'].map(item=>this[item]=this[item].bind(this)); this.state={ value:null, tableData:[], linkModel:false, visible:false, isColor:false, myDisabled:false, isEdit:true, isFace:false, isBackground:false, linkValue:null, editStatus:[ {label:'加粗',value:'bold',icon:'zitijiacu'}, {label:'斜体',value:'italic',icon:'zitixieti'}, {label:'下划线',value:'underline',icon:'xiahuaxian'}, {label:'链接',value:'createLink',icon:'lianjie'} ], fontSizeData:[ {title:'大号',value:'h1',icon:'H1'}, {title:'中号',value:'h2',icon:'h2'}, {title:'正常',value:'h3',icon:'h3-copy-copy'}, {title:'小号',value:'h4',icon:'h4'} ], isSent:true, colorData:[ 'red','orange','yellow','#01FF01','#98F5FF','#8686FF','rgb(216, 154, 255)', '#fff', '#DE1607','#E49402','#E2E205','#04DE04','rgb(71, 237, 255)','#6363F9','rgb(204, 123, 255)', 'rgb(206, 205, 205)', '#C10303','#D08702','#C5C503','#07C307','rgb(0, 221, 245)','#4C4CFB','rgb(184, 70, 255)', 'rgb(183, 183, 183)', '#960505','#AB7005','#ABAB03','#02A902','rgb(6, 171, 189)','#3333FF','rgb(167, 25, 255)', 'rgb(148, 148, 148)', '#710303','#989805','#989805','#059C05','rgb(9, 138, 152)','blue','#A020F0', 'rgb(76, 75, 75)', '#5D0404',' #757504','#757504','green','rgb(2, 99, 109)','blue','#A020F0', '#000','rgb(56, 2, 2)' ], } }; componentDidMount(){ document.addEventListener('click',this.documentClick); }; componentWillReceiveProps(nextProps){ if('value' in nextProps&&this;.state.editValue !== nextProps.value){ this.setState({editValue:nextProps.value}) } } //全局取消隐藏颜色框 documentClick=(e)=>{ const {isColor,isBackground} = this.state; if(isColor||isBackground){ let en = e.srcElement||e.target; const name = '.color-content'; while(en){ if(en.className&&en;.className === name.replace('.','')){ return; } en = en[removed]; } this.setState({isColor:false,isBackground:false}); } }; //卸载颜色框 componentWillUnmount(){ document.removeEventListener('click',this.documentClick) } /* * <粘贴功能> * @param onParseOrDrop 通用方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值