1.给文本设置行高
<template>
<text id='text' style='line-height: 30rpx;'></text>
</template>
2.计算文本行数
此处需要获取节点元素的高度,所以必须要在mounted中去写
mounted() {
// 获取文字所在元素的高度
uni.createSelectorQuery().select('#text').boundingClientRect(function(rect) {
// 获取文字的行数
let lineHeight = parseInt(rect.height / uni.upx2px(30)); //此处的30是文本的行高
// 判断文字是否超出五行
if (lineHeight > 2) {
console.log('文字超出2行');
} else {
console.log('文字未超出2行');
}
}).exec();
},