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 小时前
鸿蒙开发笔记-15-应用启动框架AppStartup
android·华为·harmonyos
HarmonyOS_SDK2 小时前
打造用户认证系统,构筑信息安全防线
harmonyos
__Benco2 小时前
OpenHarmony子系统开发 - init启动引导组件(八)
人工智能·harmonyos
二流小码农3 小时前
鸿蒙开发:了解Canvas绘制
android·ios·harmonyos
祖师爷科技4 小时前
鸿蒙项目源码-记账本app个人财物管理-原创!原创!原创!
华为·harmonyos
_唐浮8 小时前
【HarmonyOS Next】Promise你可能理解错了
harmonyos·arkts
妮妮分享9 小时前
维智 Harmony NEXT 定位 SDK:鸿蒙生态下的空间智能应用实践
人工智能·华为·ai编程·harmonyos
二流小码农9 小时前
鸿蒙开发:动态添加节点
android·ios·harmonyos
小藤神9 小时前
鸿蒙ArkTS 一次开发 多端部署 三层架构 LazyForEach懒加载 Account Kit华为一键登录
前端·harmonyos·arkts