中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何通过TextStyle实现一段丰富的文字?
微信扫码分享
import { LengthMetrics } from '@kit.ArkUI'; @Entry @Component struct Index { textStyleAttrs: TextStyle = new TextStyle({ fontWeight: FontWeight.Bolder, fontSize: LengthMetrics.vp(24), fontStyle: FontStyle.Italic }) mutableStyledString: MutableStyledString = new MutableStyledString("运动35分钟 目标达成", [ { start: 2, length: 2, styledKey: StyledStringKey.FONT, styledValue: this.textStyleAttrs }, { start: 7, length: 4, styledKey: StyledStringKey.FONT, styledValue: new TextStyle({ fontColor: Color.Orange, fontSize: LengthMetrics.vp(12)}) } ]); controller: TextController = new TextController(); onPageShow(): void { this.controller.setStyledString(this.mutableStyledString) } build() { Column() { // 显示属性字符串 Text(undefined, { controller: this.controller }) .margin({ top: 10 }) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) } }