文档管理中心

您当前浏览的3.1/4.0版本文档归档不再维护,推荐您使用最新的HarmonyOS NEXT版本文档。详细请参考文档维护策略变更

tspan

添加文本样式。

说明
  • 该组件从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

  • 文本的展示内容需要写在元素标签内,可嵌套子元素标签tspan分段。

  • 文本分段,只支持被父元素标签svg嵌套。

权限列表

子组件

支持tspan

支持以下表格中的属性。

展开
名称 类型 默认值 必填 描述
id string - 组件的唯一标识。
x <length>|<percentage> 0 设置组件左上角x轴坐标。
y <length>|<percentage> 0 设置组件左上角y轴坐标。作为textpath子组件时失效。
dx <length>|<percentage> 0 设置文本x轴偏移。
dy <length>|<percentage> 0 设置文本y轴偏移。作为textpath子组件时失效。
rotate number 0 字体以左下角为圆心旋转角度,正数顺时针,负数逆时针。
font-size <length> 30px 设置文本的尺寸。
fill <color> black 字体填充颜色。
opacity number 1 元素的透明度,取值范围为0到1,1表示为不透明,0表示为完全透明。支持属性动画。
fill-opacity number 1.0 字体填充透明度。
stroke <color> black 绘制字体边框并指定颜色。
stroke-width number 1px 字体边框宽度。
stroke-opacity number 1.0 字体边框透明度。

示例

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <svg >
  4. <text x="20" y="500" fill="#D2691E" font-size="20">
  5. zero text.
  6. <tspan>first span.</tspan>
  7. <tspan fill="red" font-size="35">second span.</tspan>
  8. <tspan fill="#D2691E" font-size="40" rotate="10">third span.</tspan>
  9. </text>
  10. <text x="20" y="550" fill="#D2691E" font-size="20">
  11. <tspan dx="-5" fill-opacity="0.2">first span.</tspan>
  12. <tspan dx="5" fill="red" font-size="25" fill-opacity="0.4">second span.</tspan>
  13. <tspan dy="-5" fill="#D2691E" font-size="35" rotate="-10" fill-opacity="0.6">third span.</tspan>
  14. <tspan fill="#blue" font-size="40" rotate="10" fill-opacity="0.8" stroke="#00FF00" stroke-width="1px">forth span.</tspan>
  15. </text>
  16. </svg>
  17. </div>
收起
自动换行
深色代码主题
复制
  1. /* xxx.css */
  2. .container {
  3. flex-direction: row;
  4. justify-content: flex-start;
  5. align-items: flex-start;
  6. height: 1000px;
  7. width: 1080px;
  8. }

属性动画示例

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <svg>
  4. <text y="300" font-size="30" fill="blue">
  5. <tspan>
  6. tspan attribute x|opacity|rotate
  7. <animate attributeName="x" from="-100" to="400" dur="3s" repeatCount="indefinite"></animate>
  8. <animate attributeName="opacity" from="0.01" to="0.99" dur="3s" repeatCount="indefinite"></animate>
  9. <animate attributeName="rotate" from="0" to="360" dur="3s" repeatCount="indefinite"></animate>
  10. </tspan>
  11. </text>
  12. <text y="350" font-size="30" fill="blue">
  13. <tspan>
  14. tspan attribute dx
  15. <animate attributeName="dx" from="-100" to="400" dur="3s" repeatCount="indefinite"></animate>
  16. </tspan>
  17. </text>
  18. </svg>
  19. </div>
收起
自动换行
深色代码主题
复制
  1. /* xxx.css */
  2. .container {
  3. flex-direction: row;
  4. justify-content: flex-start;
  5. align-items: flex-start;
  6. height: 3000px;
  7. width: 1080px;
  8. }

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <svg>
  4. <text>
  5. <tspan x="0" y="550" font-size="30">
  6. tspan attribute fill|fill-opacity
  7. <animate attributeName="fill" from="blue" to="red" dur="3s" repeatCount="indefinite"></animate>
  8. <animate attributeName="fill-opacity" from="0.01" to="0.99" dur="3s" repeatCount="indefinite"></animate>
  9. </tspan>
  10. </text>
  11. </svg>
  12. </div>

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <svg>
  4. <text>
  5. <tspan x="20" y="600" fill="red">
  6. tspan attribute font-size
  7. <animate attributeName="font-size" from="10" to="50" dur="3s" repeatCount="indefinite"></animate>
  8. </tspan>
  9. </text>
  10. </svg>
  11. </div>

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <svg>
  4. <text>
  5. <tspan x="20" y="650" font-size="25" fill="blue" stroke="red">
  6. tspan attribute stroke
  7. <animate attributeName="stroke" from="red" to="#00FF00" dur="3s" repeatCount="indefinite"></animate>
  8. </tspan>
  9. </text>
  10. <text>
  11. <tspan x="300" y="650" font-size="25" fill="white" stroke="red">
  12. tspan attribute stroke-width-opacity
  13. <animate attributeName="stroke-width" from="1" to="5" dur="3s" repeatCount="indefinite"></animate>
  14. <animate attributeName="stroke-opacity" from="0.01" to="0.99" dur="3s" repeatCount="indefinite"></animate>
  15. </tspan>
  16. </text>
  17. </svg>
  18. </div>

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