针对包含文本元素的组件(比如:Text、Span、Button、TextInput等),可以设置一些通用的文本样式,比如颜色:fontColor、大小:fontSize、样式:fontStyle、 粗细:fontWeight、字体:fontFamily
文本组件,可以包含子组件Span
@Entry @Component struct TextPage { build() { Row() { Column() { Text("默认样式Text组件") Text("可设置基础文本样式") .fontColor(Color.Red) .fontStyle(FontStyle.Italic) .fontSize(30) .fontWeight(FontWeight.Bold) .fontFamily("Arial,sans-serif") } .width('100%') } .height('100%') } }
@Entry @Component struct TextPage { build() { Row() { Column() { Text("水平对齐尾部") .width('100%') .backgroundColor(Color.Yellow) .textAlign(TextAlign.End) } .width('100%') } .height('100%') } }
设置文本的对齐方式
参数类型为TextAlign,可选类型:
用于设置文本显示最大行数。
设置文本截取方式
.textOverflow({overflow:TextOverflow.Ellipsis}) .maxLines(1)
当文本内容较多超出了组件范围时,用来设置文本截取方式,需配合maxLines使用,单独设置不生效
参数类型为TextOverflow,可设置类型:
设置文本装饰线样式及其颜色
.decoration({ type: TextDecorationType.Underline, color: Color.Red })
设置文本装饰线样式,使用TextDecorationType设置,可设置的类型:
使用Color设置本文装饰线颜色
图片组件,可以给Image设置图片地址、宽和高
@Entry @Component struct ImagePage { build() { Row() { Column() { Image($r("app.media.icon")) .width(100) .height(100) } .width('100%') } .height('100%') } }
设置图片的缩放类型
.objectFit(ImageFit.Contain)
使用ImageFit设置,可设置类型:
Image('https://网络图片.jpg')
需要在module.json5文件中申明网络访问权限
{ "module" : { "requestPermissions":[ { "name": "ohos.permission.INTERNET" } ] } }
输入单行文本
@Entry @Component struct TextInputPage { build() { Row() { Column() { TextInput() .fontColor(Color.Blue) .fontSize(20) .fontStyle(FontStyle.Italic) .fontWeight(FontWeight.Bold) .fontFamily('Arial') } .width('100%') } .height('100%') } }
@Entry @Component struct TextInputPage { build() { Row() { Column() { TextInput({ placeholder: '请输入账号' }) .placeholderColor(0x999999) .placeholderFont({ size: 20, weight: FontWeight.Medium, family: 'cursive', style: FontStyle.Italic }) } .width('100%') } .height('100%') } }
type的参数类型为InputType,包含的输入类型:
使用TextInputController的caretPosition方法
创建一个TextInputController对象,将这个对象设置到TextInput组件中,当点击按钮时,如果输入的字符数满足条件,光标会移动到caretPosition设置的position中
@Entry @Component struct TextInputPage { controller: TextInputController = new TextInputController() build() { Row() { Column() { TextInput({ controller: this.controller }) .type(InputType.Email) Button('设置光标位置') .onClick(() => { this.controller.caretPosition(2) }) } .width('100%') } .height('100%') } }
可以给TextInput设置onChange事件,输入文本发生变化时触发回调
@Entry @Component struct TextInputPage { @State text: string = '' build() { Row() { Column() { TextInput({ placeholder: '请输入账号' }) .onChange((value:string) => { this.text = value }) Text(this.text) } .width('100%') } .height('100%') } }
修改光标颜色
.caretColor(Color.Red)
用来响应点击事件操作,可以包含子组件
实现一个登录按钮
@Entry @Component struct ButtonPage { build() { Row() { Column() { Button('登录', { type: ButtonType.Capsule, stateEffect: true }) .width('90%') .height(40) .fontSize(16) .fontWeight(FontWeight.Medium) .backgroundColor('#007DFF') } .width('100%') } .height('100%') } }
用ButtonType来设置按钮样式,可设置的类型有:
用stateEffect来设置按钮是否开启切换效果(闪动),false时,点击效果关闭,默认值为true。
Button('登录', { type: ButtonType.Capsule, stateEffect: true })
用onClick来给按钮绑定点击事件
.onClick(()=>{ // 处理点击事件逻辑 })
@Entry @Component struct ButtonPage { build() { Row() { Column() { Button({ type: ButtonType.Capsule, stateEffect: true }) { Row() { Image($r('app.media.icon')) .width(30) .height(30) Text('登录') .fontSize(20) .fontColor(Color.White) } } .width('90%') .height(40) .backgroundColor(Color.Red) } .width('100%') } .height('100%') } }
进度条组件
可以设置颜色color,和宽width、高height
@Entry @Component struct LoadingProgressPage { build() { Row() { Column() { LoadingProgress() .color(Color.Blue) .height(60) .width(60) } .width('100%') } .height('100%') } }