微信小程序可以通过监听元素的高度进行文本自适应的展开收起,建议封装成组件,以便使用
先看效果


<!--components/adjustableText/adjustableText.wxml-->
<text class="content {{ isShowMore ? 'content-open' : 'overlines-hidden' }}" style="font-size: {{ fontSize }}rpx;color: {{ fontColor }};{{ isShowMore ? '' : '-webkit-line-clamp:' + n + ';' }}" selectable="{{ selectable }}">{{ content }}</text>
<block wx:if="{{ showShowMoreBtn }}">
  <view wx:if="{{ !isShowMore }}" style="margin-top: 20rpx;" class="col center">
    <view class="row center"  bindtap="showMore">
      <view style="font-size: {{ btnFontSize }}rpx;color: {{ btnFontColor }};">展开</view>
      <image src="/assets/icons/arrow/arrow_bottom.png" style="width: 32rpx;height: 32rpx;"></image>
    </view>
  </view>
  <view wx:if="{{ isShowMore }}" style="margin-top: 20rpx;" class="col center">
    <view class="row center"  bindtap="showMore">
      <view style="font-size: {{ btnFontSize }}rpx;color: {{ btnFontColor }};">收起</view>
      <image src="/assets/icons/arrow/arrow_bottom.png" style="width: 32rpx;height: 32rpx;transform: rotate(180deg);"></image>
    </view>
  </view>
</block>// components/adjustableText/adjustableText.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    content: String,
    n: {
      type: Number,
      value: 2
    },
    fontSize: {
      type: Number,
      value: 28
    },
    fontColor: {
      type: String,
      value: '#333333'
    },
    btnFontSize: {
      type: Number,
      value: 24
    },
    btnFontColor: {
      type: String,
      value: '#333333'
    },
    selectable: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    isShowMore: true, // 默认展开 以便于判断区域高度
    showShowMoreBtn: false,
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.createSelectorQuery().selectAll(".content").boundingClientRect(res => {
        if (res[0].height > this.properties.n * this.properties.fontSize / 2) {
          this.setData({
            isShowMore: false,
            showShowMoreBtn: true
          })
        }
      }).exec()
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showMore: function (e) {
      this.setData({
        isShowMore: !this.data.isShowMore
      })
    },
  },
})
.row {
  display: flex;
  flex-direction: row;
  box-sizing: border-box;
}
.col {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}
.sb {
  justify-content: space-between;
}
.center {
  align-items: center;
}
/*  */
.content {
  margin-top: 24rpx;
  width: 656rpx;
  line-height: 34rpx;
  color: #BFBFBF;
}
.content-open {
  display: block;
}
.overlines-hidden {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  /* -webkit-line-clamp: 2; */
  overflow:hidden;
  /*! autoprefixer: off */
  -webkit-box-orient: vertical;
}在需要使用的page的.json文件将文件引入
"usingComponents": {
    "adjustable-text": "/components/adjustableText/adjustableText"
  },在要使用的page中
<adjustable-text content="这是一段测试文本" n="{{ 2 }}" font-size="{{ 28 }}" font-color="#BFBFBF" btn-font-size="{{ 24 }}" btn-font-color="#BFBFBF" selectable="{{ false }}"></adjustable-text>content: 文本内容n: 超过n行后收缩,默认2行font-size: 文本的尺寸,单位rpx,默认28rpxfont-color: 文本的颜色,默认#333333btn-font-size: 收起展开按钮的文字大小,单位rpx,默认24rpxbtn-font-color: 收起展开按钮的文字颜色,默认#333333selectable: 是否可以选择文本,用于允许选择复制文本的情形,默认为false组件用到的箭头图片也给大家一起放这了
