文档管理中心
FAQ应用框架开发UI框架组件使用Text组件嵌套多个Span组件的布局效果示例

Text组件嵌套多个Span组件的布局效果示例

问题现象

开发者使用Text组件中嵌套多个Span组件时,想要实现以下两个效果:

  • 效果一:需要给Span组件设置最大的字符数,超出部分用省略号显示。
  • 效果二:其中一个Span组件排不下一行时可以自动换行不会被截断。

背景知识

解决方案

  • 场景一:单独实现以上两个效果。
    • 针对效果一:可以使用if()条件渲染方法实现。
      收起
      自动换行
      深色代码主题
      复制
      1. @Entry
      2. @Component
      3. struct CombinedSpanExampleOne {
      4. testInfo: string = '旨在指导开发者和设计师打造统一';
      5. build() {
      6. RelativeContainer() {
      7. Text() {
      8. if (this.testInfo.length > 5) {
      9. Span(this.testInfo.slice(0, 5) + ' ... ');
      10. } else {
      11. Span(this.testInfo);
      12. }
      13. Span('从理念到实现');
      14. Span('好的用户体验');
      15. Span('合理设计理念');
      16. Span('完美视觉风格');
      17. Span('交互事件归一');
      18. }
      19. .fontSize(14);
      20. }
      21. .height('100%')
      22. .width('85%')
      23. .margin({ left: 30, top: 220 });
      24. }
      25. }

      效果预览:

    • 针对效果二:先获取当前屏幕宽度,再计算指定文本的宽度,将两者对比判断是否需要换行,如果需要换行,则插入一个换行符来实现。
      收起
      自动换行
      深色代码主题
      复制
      1. import display from '@ohos.display';
      2. import { MeasureUtils } from '@kit.ArkUI';
      3. @Entry
      4. @Component
      5. struct CombinedSpanExampleTwo {
      6. msg: string = '为你提供全端侧的针对性设计建议';
      7. message: string = '和谐的数字世界';
      8. msgAdd: string = '共同构建一个和谐的数字世界';
      9. uiContext: UIContext = this.getUIContext();
      10. uiContextMeasure: MeasureUtils = this.uiContext.getMeasureUtils();
      11. textWidth: number = display.getDefaultDisplaySync().width - this.getUIContext().vp2px(60);
      12. build() {
      13. Column() {
      14. Row() {
      15. Text() {
      16. Span(this.msg).fontColor('#ff2f69ff').fontSize(14);
      17. if (this.changeLine(this.msg, this.message)) {
      18. Span('\n');
      19. }
      20. Span(this.message).fontColor(Color.Black).fontSize(14);
      21. }
      22. .textAlign(TextAlign.Start)
      23. .width('100%')
      24. .backgroundColor('#ffe9e9e9');
      25. }
      26. .width('100%')
      27. .padding({ left: 30, right: 38 })
      28. .height(200);
      29. }
      30. .width('100%')
      31. .height('100%')
      32. .justifyContent(FlexAlign.Center)
      33. .alignItems(HorizontalAlign.Center);
      34. }
      35. changeLine(msg1: string, message: string): boolean {
      36. let i: number = 0;
      37. let j: number = 0;
      38. while (j < msg1.length) {
      39. j++;
      40. if (this.getTextWidth(msg1.substring(i, j), 14) > this.textWidth) {
      41. i = j - 1;
      42. }
      43. }
      44. console.info(`msgAdd ${this.msgAdd}`);
      45. return this.getTextWidth(msg1.substring(i), 14) + this.getTextWidth(message, 14) > this.textWidth;
      46. }
      47. getTextWidth(content: string, fontSize: number): number {
      48. const width = this.uiContextMeasure.measureText({
      49. textContent: content,
      50. fontSize: fontSize
      51. });
      52. console.error(`getTextWidth: content: ${content}, width: ${width}`);
      53. return width;
      54. }
      55. }

      当changeLine方法传入的文本(msgAdd)和msg加起来超过一行时,会进行换行。

      换行效果预览:

      当changeLine方法传入的文本(message)和msg加起来不超过一行时,不会进行换行。

      不换行效果预览:

  • 场景二:将以上两个效果结合在一起实现。使用substring()方法截取指定长度字符,通过三元运算符来动态截断文本换行并添加省略号实现以上两个效果。
    收起
    自动换行
    深色代码主题
    复制
    1. @Entry
    2. @Component
    3. struct CombinedSpanExampleThree {
    4. longText: string =
    5. '第一行需要截断的文本内容结束第二行开始需要自动换行的长文本第三行要显示全部的文本部分且没有省略号不会被截断可以换行';
    6. build() {
    7. Column() {
    8. Text() {
    9. Span(this.getTruncatedText(14, 0))
    10. .fontColor(Color.Blue);
    11. Span(this.getTruncatedText(15, 14))
    12. .fontColor(Color.Black)
    13. .textBackgroundStyle({ color: Color.Yellow });
    14. Span(this.getTruncatedText(Infinity, 29))
    15. .fontColor(Color.Green);
    16. }
    17. .width('80%')
    18. .margin({ top: 20 })
    19. .textOverflow({ overflow: TextOverflow.None })
    20. .backgroundColor('#FFEAEAEA');
    21. }
    22. .width('100%')
    23. .height('100%')
    24. .alignItems(HorizontalAlign.Center)
    25. .justifyContent(FlexAlign.Center);
    26. }
    27. private getTruncatedText(maxLength: number, startIndex: number): string {
    28. const targetText = this.longText.substring(startIndex);
    29. return targetText.length > maxLength ?
    30. targetText.substring(0, maxLength) + '...' + '\n' :
    31. targetText;
    32. }
    33. }

    效果预览:

常见FAQ

Q:Text组件如何实现以字母为单位进行截断效果。

A:通过wordBreak属性设置断行规则WordBreak.BREAK_ALL即可实现字母为单位进行截断效果。可以参考示例4(设置文本断行及折行)

Q:Span组件如何设置背景色。

A:可以添加textBackgroundStyle属性来给Span组件设置背景色。

在 FAQ 中进行搜索
请输入您想要搜索的关键词