3.5、HarmonyOS Next 文本显示(Text/Span)

创建文本

Text 可通过以下两种方式来创建:

string 字符串

效果图

ts 复制代码
Text('我是一段文本')

引用 Resource 资源

资源引用类型可以通过 $r 创建 Resource 类型对象,文件位置为 /resources/base/element/string.json。

引用的资源位于:src/main/resources/base/element/string.json 对应代码

ts 复制代码
Text($r('app.string.module_desc'))
  .baselineOffset(0)
  .fontSize(30)
  .border({ width: 1 })
  .padding(10)
  .width(300)

添加子组件

Span 只能作为 Text 组件的子组件显示文本内容。可以在一个 Text 内添加多个 Span 来显示一段信息,例如产品说明书、承诺书等。

创建 Span

Span 组件需要写到 Text 组件内,单独写 Span 组件不会显示信息,TextSpan 同时配置文本内容时,Span 内容覆盖 Text 内容。

ts 复制代码
Text('我是Text') {
  Span('我是Span')
}
.padding(10)
.borderWidth(1)

设置文本装饰线及颜色。

通过decoration设置文本装饰线及颜色。

ts 复制代码
Text() {
  Span('我是Span1,').fontSize(16).fontColor(Color.Grey)
    .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
  Span('我是Span2').fontColor(Color.Blue).fontSize(16)
    .fontStyle(FontStyle.Italic)
    .decoration({ type: TextDecorationType.Underline, color: Color.Black })
  Span(',我是Span3').fontSize(16).fontColor(Color.Grey)
    .decoration({ type: TextDecorationType.Overline, color: Color.Green })
}
.borderWidth(1)
.padding(10)

通过textCase设置文字一直保持大写或者小写状态。

ts 复制代码
Text() {
  Span('I am Upper-span').fontSize(12)
    .textCase(TextCase.UpperCase)
}
.borderWidth(1)
.padding(10)
.margin({left:10})

添加事件。

由于Span组件无尺寸信息,事件仅支持点击事件onClick。

ts 复制代码
Text() {
	 Span('I am Upper-span').fontSize(12)
     .textCase(TextCase.UpperCase)
     .onClick(()=>{
       console.info('我是Span------onClick')
     })
 }

自定义文本样式

通过 textAlign 属性设置文本对齐样式。

ts 复制代码
Column() {
	Text('左对齐')
	  .width(300)
	  .textAlign(TextAlign.Start)
	  .border({ width: 1 })
	  .padding(10)
	Text('中间对齐')
	  .width(300)
	  .textAlign(TextAlign.Center)
	  .border({ width: 1 })
	  .padding(10)
	Text('右对齐')
	  .width(300)
	  .textAlign(TextAlign.End)
	  .border({ width: 1 })
	  .padding(10)
	  }
}

通过 textOverflow 属性控制文本超长处理,textOverflow 需配合 maxLines 一起使用(默认情况下文本自动折行)。

ts 复制代码
Column() {
   Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content. This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content.')
    .width(250)
    .textOverflow({ overflow: TextOverflow.None })
    .maxLines(1)
    .fontSize(12)
    .border({ width: 1 }).padding(10)
   Text('我是超长文本,超出的部分显示省略号。I am an extra long text, with ellipses displayed for any excess。')
    .width(250)
    .textOverflow({ overflow: TextOverflow.Ellipsis })
    .maxLines(1)
    .fontSize(12)
    .border({ width: 1 }).padding(10)
}
.margin({left:10})

通过 lineHeight 属性设置文本行高。

ts 复制代码
Column() {
  Text('This is the text with the line height set. This is the text with the line height set.')
    .width(300).fontSize(12).border({ width: 1 }).padding(10)
  Text('This is the text with the line height set. This is the text with the line height set.')
    .width(300).fontSize(12).border({ width: 1 }).padding(10)
    .lineHeight(20)
}
.margin({left:10})

通过 decoration 属性设置文本装饰线样式及其颜色。

ts 复制代码
Column() {
  Text('This is the text')
     .decoration({
       type: TextDecorationType.LineThrough,
       color: Color.Red
     })
     .borderWidth(1).padding(10).margin(5)
   Text('This is the text')
     .decoration({
       type: TextDecorationType.Overline,
       color: Color.Red
     })
     .borderWidth(1).padding(10).margin(5)
   Text('This is the text')
     .decoration({
       type: TextDecorationType.Underline,
       color: Color.Red
     })
     .borderWidth(1).padding(10).margin(5)
}

通过letterSpacing属性设置文本字符间距。

ts 复制代码
Text('This is the text content with letterSpacing 0.')
  .letterSpacing(0)
  .fontSize(12)
  .border({ width: 1 })
  .padding(10)
  .width('100%')
  .margin(5)
Text('This is the text content with letterSpacing 3.')
  .letterSpacing(3)
  .fontSize(12)
  .border({ width: 1 })
  .padding(10)
  .width('100%')
  .margin(5)
Text('This is the text content with letterSpacing -1.')
  .letterSpacing(-1)
  .fontSize(12)
  .border({ width: 1 })
  .padding(10)
  .width('100%')
  .margin(5)

通过 minFontSizemaxFontSize 自适应字体大小,minFontSize 设置文本最小显示字号,maxFontSize设置文本最大显示字号,minFontSizemaxFontSize 必须搭配同时使用,以及需配合 maxline 或布局大小限制一起使用,单独设置不生效。

ts 复制代码
Column() {
  Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为1')
    .width(250)
    .maxLines(1)
    .maxFontSize(30)
    .minFontSize(5)
    .border({ width: 1 })
    .padding(10)
    .margin(5)
  Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为2')
    .width(250)
    .maxLines(2)
    .maxFontSize(30)
    .minFontSize(5)
    .border({ width: 1 })
    .padding(10)
    .margin(5)
  Text('我的最大字号为30,最小字号为15,宽度为250,高度为50')
    .width(250)
    .height(50)
    .maxFontSize(30)
    .minFontSize(15)
    .border({ width: 1 })
    .padding(10)
    .margin(5)
  Text('我的最大字号为30,最小字号为15,宽度为250,高度为100')
    .width(250)
    .height(100)
    .maxFontSize(30)
    .minFontSize(15)
    .border({ width: 1 })
    .padding(10)
    .margin(5)
}

通过 textCase 属性设置文本大小写

ts 复制代码
 Column() {
  Text('This is the text content with textCase set to Normal.')
     .textCase(TextCase.Normal)
     .padding(10)
     .border({ width: 1 })
     .padding(10)
     .margin(5)

   // 文本全小写展示
   Text('This is the text content with textCase set to LowerCase.')
     .textCase(TextCase.LowerCase)
     .border({ width: 1 })
     .padding(10)
     .margin(5)

   // 文本全大写展示
   Text('This is the text content with textCase set to UpperCase.')
     .textCase(TextCase.UpperCase)
     .border({ width: 1 })
     .padding(10)
     .margin(5)
}

通过copyOption属性设置文本是否可复制粘贴。

ts 复制代码
Text("这是一段可复制文本")
  .fontSize(30)
   .copyOption(CopyOptions.InApp)
   .margin({bottom: 15})

添加事件

Text 组件可以添加通用事件,可以绑定 onClickonTouch 等事件来响应操作。

ts 复制代码
Text('点我')
  .onClick(()=>{
      console.info('我是Text的点击响应事件');
   })

上一篇 3.4、进度条(Progress) 下一篇 3.6、文本输入(TextInput/TextArea)

相关推荐
长弓三石1 小时前
鸿蒙网络编程系列57-仓颉版固定包头可变包体解决TCP粘包问题
网络·tcp/ip·harmonyos
zhanshuo2 小时前
HarmonyOS 隐私安全机制实战:动态权限、沙箱隔离与分布式授权
harmonyos
zhanshuo2 小时前
鸿蒙系统防黑秘籍:如何彻底防止恶意应用窃取用户数据?
harmonyos
财经三剑客6 小时前
鸿蒙智行6月交付新车52747辆 单日交付量3651辆
华为·harmonyos
睿麒6 小时前
鸿蒙app 开发中的 map 映射方式和用法
华为·harmonyos
zhanshuo8 小时前
鸿蒙 Secure Boot 全流程解析:从 BootROM 到内核签名验证的实战指南
harmonyos
zhanshuo8 小时前
鸿蒙系统安全机制全解:安全启动 + 沙箱 + 动态权限实战落地指南
harmonyos
塞尔维亚大汉8 小时前
鸿蒙内核源码分析(用户态锁篇) | 如何使用快锁Futex(上)
harmonyos·源码阅读
爱笑的眼睛1119 小时前
08-自然壁纸实战教程-视频列表-云
华为·harmonyos
Georgewu1 天前
【HarmonyOS 5】鸿蒙中自定义弹框OpenCustomDialog、CustomDialog与DialogHub的区别详解
harmonyos