零基础学鸿蒙编程ArkTS/ArkUI实战1024

33、鸿蒙/组件/文本显示 (Text/Span)

2024-07-24  本文已影响0人  圆梦人生

Text是文本组件,通常用于展示用户视图,如显示文章的文字。具体用法请参考Text

创建文本

 Text('string文本')
 Text($r('app.string.module_desc'))

添加子组件

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

Text('不会显示的文本'){
        Span("文本")
}.backgroundColor(Color.Red)
Text(){
        Span('span1').decoration({
          type: TextDecorationType.LineThrough, // 删除线
          color: Color.Red
        })
}
Text(){
        Span('span1').decoration({
          type: TextDecorationType.Underline, // 下划线
          color: Color.Red
        })
}
Text(){
        Span('span1').decoration({
          type: TextDecorationType.Overline, // 上划线
          color: Color.Red
        })
}
Text(){
        Span("i am SM").textCase(TextCase.LowerCase) // 小写
}
Text(){
        Span("i am SM").textCase(TextCase.UpperCase) // 大写
}
 Text(){
        Span('点击').onClick(()=>{
          console.log('点击');
        })
}

自定义文本样式

Text('左对齐').textAlign(TextAlign.Start).width(300).backgroundColor(Color.Red)
Text('居中对齐').textAlign(TextAlign.Center).width(300).backgroundColor(Color.Red)
Text('靠右对齐').textAlign(TextAlign.End).width(300).backgroundColor(Color.Red)
 Text('sdafasdfasdfasdfasdfasdfasdfasdfasdfasfdasdfsdafasdfasdfasdfasdfasdfasdfasdfasdfasfdasdfsdafasdfasdfasdfasdfasdfasdfasdfasdfasfdasdf')
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(2)
Text('sdafasdfasdfasdfasdfasdfasdfasdfasdfasfdasdfsdafasdfasdfasdfasdfasdfasdfasdfasdfasfdasdfsdafasdfasdfasdfasdfasdfasdfasdfasdfasfdasdf')
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(2)
.lineHeight(30)
Text('删除线').decoration({
        type: TextDecorationType.LineThrough,
        color: Color.Red
})
Text('下划线').decoration({
        type: TextDecorationType.Underline,
        color: Color.Red
})
Text('上划线').decoration({
        type: TextDecorationType.Overline,
        color: Color.Red
})
Text('This is the text content with baselineOffset 0.')
        .baselineOffset(-10)
        .fontSize(12)
        .border({ width: 1 })
        .padding(10)
        .width('100%')
        .margin(5)
 Text('sadfasdfaasdfas')
        .letterSpacing(10)
Text('sadfasdfaasdfas')
        .letterSpacing(10)
        .copyOption(CopyOptions.InApp)

-- 添加事件

Text('sadfasdfaasdfas')
        .letterSpacing(10)
        .copyOption(CopyOptions.InApp)
        .onClick((e)=>{
          console.log('被点击');
})
Text('添加边框')
        .border({
          width: 2,
          color: Color.Red,
          style: BorderStyle.Dotted
})
Text('添加左右边框')
        .border({
          width: {
            left: 1,
            right: 2
          },
          color:{
            left:  Color.Red,
            right:  Color.Blue
          },
          style: {
            left: BorderStyle.Dotted,
            right: BorderStyle.Solid
          }
})
 Text('圆角').border({
        color: Color.Red,
        style: BorderStyle.Solid,
        width: 1
}).width(100).textAlign(TextAlign.Center)
        .borderRadius(20)

Text('圆角').border({
        color: Color.Red,
        style: BorderStyle.Solid,
        width: 1
}).width(100).textAlign(TextAlign.Center)
        .borderRadius({
          topLeft: 10,
          bottomRight: 10
})

Text("圆形")
        .width(100).height(100)
        .border({
          color: Color.Red,
          style: BorderStyle.Solid,
          width: 1
}).textAlign(TextAlign.Center)
        .borderRadius(50)
 Text('背景图')
        .width(100).height(100)
        .backgroundColor(Color.Red)
        .backgroundImage($r('app.media.app_icon'), ImageRepeat.NoRepeat)
        .backgroundImagePosition({
          x: 10,
          y: 20
})
// .backgroundImagePosition(Alignment.Center) // 居中
.backgroundImageSize(ImageSize.Contain)
上一篇 下一篇

猜你喜欢

热点阅读