鸿蒙历史搜索功能:tag标签根据文字宽度自动换行 展示更多

一.需求

历史搜索功能,需要根据文字宽度自动换行,并且只展示3行,文字宽度不固定

二.思路

1.通过鸿蒙api MeasureText.measureText 先获取文字宽度

2.通过Row组件的onAreaChange事件监听当前组件高度变化

三.实现

监听需要展示的数量,是否返回完整的数据和需要截取的数量
定义需要用到的变量 如果是v1的话@Local换成@State即可
TypeScript 复制代码
@Local isExpanded: boolean = false // 是否继续计算行高
@Local visibleItemCount: number = 0; // 当前可见的标签数量
@Local arr:string[] = ['123','123','123','123','123','123','123','123']; 
//监听需要展示数量
@Computed
  get displayList() {
    return this.isExpanded
      ? this.arr
      : this.arr.slice(0, this.visibleItemCount);
  }
标签样式和布局样式
TypeScript 复制代码
// 标签样式
  @Builder
  tagView(item: string) {
    Column() {
      Text(item)
        .fontSize(14)
        .fontWeight(400)
        .fontColor('#666666')
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .maxLines(1)
    }
    .onClick(() => {
      
    })
    .height(32)
    .borderRadius(6)
    .backgroundColor('#F6F7F8')
    .margin({ top: 8, right: 8 })
    .padding({
      left: 12,
      right: 12,
      top: 6,
      bottom: 6
    })
  }

// 布局和监听高度判断
 Column({ space: '8vp' }) {
          Flex({
            direction: FlexDirection.Row, // 横向排列
            wrap: FlexWrap.Wrap, // 自动换行
            justifyContent: FlexAlign.Start, // 左对齐
            alignItems: ItemAlign.Center  // 垂直居中对齐
          }) {
            ForEach(this.displayList), (item: string) => {
              this.tagView(item); // ui样式抽成了Builder,换成自己的就行
            }, (item: string, index: number) => JSON.stringify(item) + index);
            // 展示更多按钮
            if (this.isExpanded) {
              this.moreButton();
            }
          }
         .onAreaChange((oldValue: Area, newValue: Area) => {
            if (this.isExpanded) {
              return; // 已经点击"更多"按钮,直接返回,不再进行计算
            }
            const containerWidth = newValue.width; // 当前页面宽度用来存放标签的总宽度
// 最大高度  这里因为我每行高度是固定40,只展示3行所以是120,可以根据自己需求调整
            const MAX_HEIGHT = 120; 
            if (newValue.height >= MAX_HEIGHT) {
              this.showMoreButton = true; // 显示 "更多" 按钮
              const visibleItemsCount = this.calculateVisibleItems2(Number(containerWidth), MAX_HEIGHT);
              this.visibleItemCount = visibleItemsCount; // 更新当前可见标签数量
            } else {
              this.showMoreButton = false; // 隐藏 "更多" 按钮
              this.visibleItemCount = this.arr.length; // 显示所有标签
            }
          });
        }
高度和宽度计算
TypeScript 复制代码
  //计算显示几个标签 通过行高,最大行数,标签动态宽度
  calculateVisibleItems2(containerWidth: number, maxHeight: number): number {
    const LINE_HEIGHT = 40; // 每行固定高度
    const MAX_LINES = Math.floor(maxHeight / LINE_HEIGHT); // 最大行数
    let currentWidth = 0; // 当前行的宽度累加
    let currentLine = 1; // 当前是第几行
    let visibleCount = 0; // 可见的标签数量
    for (let item of this.vm.searchHistoryList) {
      const itemWidth = this.getTagWidth(item); // 获取标签宽度
      if (currentWidth + itemWidth <= containerWidth) {
        // 标签可以放入当前行
        currentWidth += itemWidth;
      } else {
        // 换行
        currentLine++;
        if (currentLine > MAX_LINES) {
          break; // 超过最大行数,停止计数
        }
        currentWidth = itemWidth; // 新行的第一个标签宽度
      }
      visibleCount++; // 增加可见的标签数量
    }
    return visibleCount;
  }
展示更多按钮
TypeScript 复制代码
@Builder
  moreButton() {
    Column() {
      Text('展示更多')
    }
    .height(32)
    .width(32)
    .backgroundColor('#F6F7F8')
    .borderRadius(6)
    .margin({ top: 8, right: 8 })
    .padding({
      left: 12,
      right: 12,
      top: 6,
      bottom: 6
    })
    .onClick(() => {
     this.isExpanded = true; // 标记为已展开
     this.visibleItemCount = this.arr.length; // 更新为显示所有标签
    })
  }
相关推荐
wangxiaowu19869 小时前
HarmonyOS NEXT和通用JSBridge
华为·harmonyos
cz追天之路13 小时前
华为机考 ------ 识别有效的IP地址和掩码并进行分类统计
javascript·华为·typescript·node.js·ecmascript·less·css3
航Hang*14 小时前
第五章:网络系统建设与运维(中级)——生成树协议
运维·服务器·网络·笔记·华为·ensp
航Hang*18 小时前
第三章:网络系统建设与运维(中级)——交换技术
运维·笔记·计算机网络·华为·ensp·交换机
l1340620823518 小时前
Flutter Geocoding 在鸿蒙上的使用指南
flutter·华为·harmonyos
无人装备硬件开发爱好者18 小时前
华为海思 BS21E (H2821E) 星闪组网测距定位 技术可行性方案
华为·最小二乘法·星闪·测距定位
俩毛豆19 小时前
【毛豆工具集】【UI】【多设备适配】实现与屏幕密度等倍的图片加载
华为·harmonyos
l1340620823519 小时前
344.在鸿蒙上使用 animations Flutter 包的指南
flutter·华为·harmonyos
灯前目力虽非昔,犹课蝇头二万言。20 小时前
HarmonyOS笔记12:生命周期
笔记·华为·harmonyos
Random_index1 天前
#HarmonyOS篇:学习UI规范基本语法&&学习UI范式装填管理V1&&学习UI范式装填管理V2&&学习UI范式渲染控制
harmonyos