HarmonyOS 多行文本末尾带标签时的缩进

现在存在多行文本,maxLines为2,文本末尾携带一个标签,遮盖标签一直存在,当文本末尾的文字即将到达标签区域时,直接剪切掉,类似于图片的样式,有什么实现方案么

HarmonyOS 多行文本末尾带标签时的缩进 -鸿蒙开发者社区

HarmonyOS
2025-01-09 15:15:17
368浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

请参考demo:

//Index.ets
import measure from '@ohos.measure';
import { Tags } from './Tags';

function computeTextIndent(text:string):string{
  const sizeOptions:SizeOptions = measure.measureTextSize({
    textContent: text,
    fontSize: '10px'
  });
  let width = sizeOptions.width;
  if(typeof width === 'string'){
    return width.slice(0,width.length-2) + 10 + 'vp';
  }
  if(typeof width === 'number'){
    return width + 10 + 'vp';
  }
  return 0 + 'vp';
}

@Entry
@Component
struct Index {

  @Builder OverlayNode() {
    Column() {
      Tags({ tagsText: '新品新品', tagsType: 'newProduct' })
    }
  }

  build() {
    Column() {
      Row() {
        Text('这里是标题这里是标题这里是标题这里是标题这里是标题这里是标题这里是标题')
          .maxLines(2)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .overlay(this.OverlayNode(), {
            offset: { x: 150, y: 20 } })
      }
      .height('100%')
      .width('100%')
    }
    .height('100%')
    .width('100%')
    .alignItems(HorizontalAlign.Start)
  }
}

//Tags.ets
@Component
export struct Tags {
  @Prop tagsText: string;
  @Prop tagsType: string;
  @Prop isFront: boolean = false;
  @Prop frontText: string;
  @Prop frontIcon: string;
  @State TagsOptions: TagsInterface = {};

  aboutToAppear(){
    let tagsColor:TagsColor = {};
    Object.entries(TagsTypeOption).forEach((item:[string,TagsColor])=>{
      if(item[0] === this.tagsType){
        tagsColor = item[1];
      }
    })
    this.TagsOptions = {
      text:this.tagsText,
      frontText:this.frontText,
      frontIcon:this.frontIcon,
      tagsColor:tagsColor
    }
  }

  build() {
    Row() {
      if (this.isFront) {
        if(this.TagsOptions.frontText){
          Text(this.TagsOptions.frontText)
            .fontColor(this.TagsOptions.tagsColor?.color)
            .fontSize('10vp')
            .height('16vp')
            .padding('2vp')
            .backgroundColor(this.TagsOptions.tagsColor?.frontBgColor)
            .border({
              width: { right: '1vp', left:'0vp', top:'0vp', bottom:'0vp' },
              color: this.TagsOptions.tagsColor?.borderColor,
            })
        }
        if(this.TagsOptions.frontIcon){
          Image($r(this.TagsOptions.frontIcon))
            .size({ width: '14vp', height: '14vp' })
            .padding('2vp')
            .backgroundColor(this.TagsOptions.tagsColor?.frontBgColor)
            .border({
              width: { right: '1vp', left:'0vp', top:'0vp', bottom:'0vp' },
              color: this.TagsOptions.tagsColor?.borderColor,
            })
        }
      }
      Text(this.TagsOptions.text)
        .fontColor(this.TagsOptions.tagsColor?.color)
        .fontSize('10vp')
        .height('16vp')
        .padding('2vp')
        .backgroundColor(this.TagsOptions.tagsColor?.bgColor)

    }
    .height('16vp')
    .border({
      width: '1vp',
      color: this.TagsOptions.tagsColor?.borderColor,
      radius: '2vp'
    })
  }
}

class TagsInterface {
  text?:string
  frontText?:string
  frontIcon?:string
  tagsColor?:TagsColor
}
class TagsColor{
  bgColor?:string
  borderColor?:string
  color?:string
  frontBgColor?:string
}

class TagsType{
  newProduct?:TagsColor
  bonusPoint?:TagsColor
  saveMoney?:TagsColor
}

const TagsTypeOption:TagsType = {
  newProduct: {
    bgColor: "#00af3b",
    borderColor: "#00af3b",
    color: "#ffffff",
    frontBgColor: "",
  },
  bonusPoint:{
    bgColor: "#feffff",
    borderColor: "#fa8027",
    color: "#f2c5a0",
    frontBgColor: "#fff7f4",
  },
  saveMoney:{
    bgColor: "#feffff",
    borderColor: "#ffdada",
    color: "#ff0005",
    frontBgColor: "#ffeded",
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.

您这边可以参考上面的demo,对标签位置和大小进行调整,以达到遮盖整个文字而非有半个文字露出在外面的效果。

或者参考该demo,使用stack组件达成遮盖效果:

https://developer.huawei.com/consumer/cn/forum/topic/0208153595060533866?fid=0109140870620153026

目前并不支持对限制最大行数以后的文本进行剪切,您可以尝试参考以下demo,减少接收文字字符数,以达到类似的效果:

https://developer.huawei.com/consumer/cn/forum/topic/0208151014948857136?fid=0109140870620153026

分享
微博
QQ
微信
回复
2025-01-09 18:13:47


相关问题
多行文字后面添加标签
675浏览 • 1回复 待解决
多行文本省略展开与显示
1676浏览 • 1回复 待解决
HarmonyOS 计算多行文本布局宽高
640浏览 • 1回复 待解决
HarmonyOS 多行文本中间省略不生效
402浏览 • 1回复 待解决
HarmonyOS Text多行文本不能居中对齐
1687浏览 • 1回复 待解决
Text怎么显示html标签文本
5271浏览 • 1回复 待解决
Text怎么解析展示html标签文本
2683浏览 • 1回复 待解决
如何对文本实现首行缩进
724浏览 • 1回复 待解决
HarmonyOS Html文本标签解析器
580浏览 • 1回复 待解决
HarmonyOS 命令行进行文本输入
613浏览 • 1回复 待解决
如何实现标签文本换行
1554浏览 • 1回复 待解决
HarmonyOS 如何进行文本文件读取?
888浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。