了解原理,其实就是 行数 = 元素高度 / 元素行高
uni-app 小程序端:
<view style="line-height:44rpx;" class="line">888888888888888888888888888888888888888</view>
uni.createSelectorQuery().in(this).select('.line').boundingClientRect(res => {
var height = res.height;
var line = height / 22 // 22 是因为单位尺寸的适配 ,行高为 44rpx , 则这里就为 22px
console.log(line,'-----行数')
}).exec();
h5 :
<view class="line-height:44rpx;" ref="line">888888888888888888888888888888888888888888</view>
var height = this.$refs['line'].$el.clientHeight;
var line = height / 22;
console.log(line,'-----行数')