之前写过一个也是自动换行, 原生实现换行, 针对iOS
- 闲来无事, 刚好有个小伙伴问我这个实现, 于是研究一番, 用weex来实现了一次,直接上图

image.png
- 主要利用weex的dom Module来获取text的高度,来动态改变,不过数字,有的时候会算不准, 有想法的可以告诉我!,注意字体需要设置成同意的字体, 要不完全算不准确, 代码如下:
<template>
<div style="flex: 1; background-color: white; align-items: center; justify-content: center;">
<textarea :style="{height: inputHeight}" class="textarea" v-model="value" @input="onInput"></textarea>
<text class="inputText" ref="inputText" style=" background-color: mediumpurple;">{{value}}</text>
</div>
</template>
<script>
export default {
data: {
value: '',
inputHeight: 80,
},
methods: {
onInput(e){
weex.requireModule('dom').getComponentRect(this.$refs.inputText, e=>{
if(e.result){
console.log(e.value,e.size.height)
if(Number(e.size.height) >= 90 || Number(e.size.height)<=200){
this.inputHeight = Number(e.size.height)
}
}
})
}
}
}
</script>
<style scoped>
.textarea{
min-height: 90px;
max-height: 200px;
width: 600px;
border-style: dashed;
border-width: 1px;
border-color: #FF0000;
border-radius: 10px;
font-size: 32px;
color: #333333;
font-family: PingFangSC-Medium;
}
.inputText {
box-sizing: border-box;
padding: 10.5px;
min-height: 70px;
max-height: 200px;
width: 600px;
font-size: 32px;
line-height: 40px;
color: #333333;
font-family: PingFangSC-Medium;
}
</style>
网友评论