当前位置:   article > 正文

鸿蒙Harmony--文本组件Text属性详解

鸿蒙Harmony--文本组件Text属性详解

金樽清酒斗十千,玉盘珍羞直万钱。 停杯投箸不能食,拔剑四顾心茫然。 欲渡黄河冰塞川,将登太行雪满山。 闲来垂钓碧溪上,忽复乘舟梦日边。 行路难,行路难,多歧路,今安在? 长风破浪会有时,直挂云帆济沧海。

目录

一,显示一个简单的文本

二,调整文字的字体大小和颜色

三,字体倾斜

四,字体加粗

1,简单的加粗

2,设置固定值加粗

五,设置文本框大小

六,设置文本框背景色

七,设置文字水平居中

八,设置背景圆角

九,添加边线

十,文字超长显示

1,超长截断

2,超长显示...

3,超长滚动

十一,设置文本行高

十二,设置文本字间距

十三,文字大小自适应

十四,英文字母统一大小写

1,统一显示大写

2,统一小写

3,正常显示

十五,设置首行文本缩进

十六,添加子组件Span

一,显示一个简单的文本

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .alignRules({
  8. top: { anchor: '__container__', align: VerticalAlign.Center },
  9. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  10. })
  11. .id('txt1')
  12. }
  13. }
  14. }

二,调整文字的字体大小和颜色

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontSize(30)
  8. .fontColor("#22aaff")
  9. .alignRules({
  10. top: { anchor: '__container__', align: VerticalAlign.Center },
  11. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  12. })
  13. .id('txt1')
  14. }
  15. }
  16. }

三,字体倾斜

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontStyle(FontStyle.Italic)//斜体
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .alignRules({
  11. top: { anchor: '__container__', align: VerticalAlign.Center },
  12. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  13. })
  14. .id('txt1')
  15. }
  16. }
  17. }

四,字体加粗

1,简单的加粗

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontWeight(FontWeight.Bold)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .alignRules({
  11. top: { anchor: '__container__', align: VerticalAlign.Center },
  12. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  13. })
  14. .id('txt1')
  15. }
  16. }
  17. }

2,设置固定值加粗

也可以通过设置数值调整加粗程度,取值为100-900

900效果为:

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .alignRules({
  11. top: { anchor: '__container__', align: VerticalAlign.Center },
  12. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  13. })
  14. .id('txt1')
  15. }
  16. }
  17. }

五,设置文本框大小

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(200) //高度
  12. .alignRules({
  13. top: { anchor: '__container__', align: VerticalAlign.Center },
  14. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  15. })
  16. .id('txt1')
  17. }
  18. }
  19. }

六,设置文本框背景色

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(200) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .alignRules({
  14. top: { anchor: '__container__', align: VerticalAlign.Center },
  15. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  16. })
  17. .id('txt1')
  18. }
  19. }
  20. }

七,设置文字水平居中

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(200) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .alignRules({
  15. top: { anchor: '__container__', align: VerticalAlign.Center },
  16. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  17. })
  18. .id('txt1')
  19. }
  20. }
  21. }

八,设置背景圆角

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(200) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .alignRules({
  16. top: { anchor: '__container__', align: VerticalAlign.Center },
  17. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  18. })
  19. .id('txt1')
  20. }
  21. }
  22. }

九,添加边线

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("袁震")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(100) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .border({width: 2, color: '#000000'}) //添加边线
  16. .alignRules({
  17. top: { anchor: '__container__', align: VerticalAlign.Center },
  18. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  19. })
  20. .id('txt1')
  21. }
  22. }
  23. }

十,文字超长显示

1,超长截断

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("英雄联盟德玛西亚之力")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(100) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .maxLines(1) //最大显示一行
  16. .border({width: 2, color: '#000000'}) //边框
  17. .textOverflow({overflow: TextOverflow.None}) //超长文字截断
  18. .alignRules({
  19. top: { anchor: '__container__', align: VerticalAlign.Center },
  20. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  21. })
  22. .id('txt1')
  23. }
  24. }
  25. }

2,超长显示...

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("英雄联盟德玛西亚之力")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(100) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .maxLines(1) //最大显示一行
  16. .border({width: 2, color: '#000000'}) //边框
  17. .textOverflow({overflow: TextOverflow.Ellipsis}) //超长显示省略号
  18. .alignRules({
  19. top: { anchor: '__container__', align: VerticalAlign.Center },
  20. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  21. })
  22. .id('txt1')
  23. }
  24. }
  25. }

3,超长滚动

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("英雄联盟德玛西亚之力")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(100) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .maxLines(1) //最大显示一行
  16. .border({width: 2, color: '#000000'}) //边框
  17. .textOverflow({overflow: TextOverflow.MARQUEE}) //超长滚动
  18. .alignRules({
  19. top: { anchor: '__container__', align: VerticalAlign.Center },
  20. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  21. })
  22. .id('txt1')
  23. }
  24. }
  25. }

十一,设置文本行高

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("英雄联盟德玛西亚之力")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(200) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .border({width: 2, color: '#000000'}) //边框
  16. .lineHeight(60) //行高
  17. .alignRules({
  18. top: { anchor: '__container__', align: VerticalAlign.Center },
  19. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  20. })
  21. .id('txt1')
  22. }
  23. }
  24. }

十二,设置文本字间距

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("英雄联盟德玛西亚之力")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(200) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .border({width: 2, color: '#000000'}) //边框
  16. .lineHeight(60) //行高
  17. .letterSpacing(20) //字间距
  18. .alignRules({
  19. top: { anchor: '__container__', align: VerticalAlign.Center },
  20. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  21. })
  22. .id('txt1')
  23. }
  24. }
  25. }

十三,文字大小自适应

注意:需要minFontSize ,maxFontSize,maxline 以及文本框大小配合使用

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("英雄联盟德玛西亚之力")
  7. .fontWeight(900)//加粗
  8. .fontSize(30)
  9. .fontColor("#22aaff")
  10. .width(200) //宽度
  11. .height(200) //高度
  12. .backgroundColor("#ccaabb") //背景色
  13. .textAlign(TextAlign.Center)//文字水平居中
  14. .borderRadius(10) //背景圆角
  15. .border({width: 2, color: '#000000'}) //边框
  16. .lineHeight(60) //行高
  17. .minFontSize(15) //最小字体15
  18. .maxFontSize(30) //最大字体30
  19. .maxLines(1) //最多显示一行
  20. .alignRules({
  21. top: { anchor: '__container__', align: VerticalAlign.Center },
  22. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  23. })
  24. .id('txt1')
  25. }
  26. }
  27. }

 

十四,英文字母统一大小写

1,统一显示大写

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("abcd")
  7. .fontWeight(900)//加粗
  8. .textCase(TextCase.UpperCase) //字母大写
  9. .fontSize(30)
  10. .fontColor("#22aaff")
  11. .width(200) //宽度
  12. .height(200) //高度
  13. .backgroundColor("#ccaabb") //背景色
  14. .textAlign(TextAlign.Center)//文字水平居中
  15. .borderRadius(10) //背景圆角
  16. .border({width: 2, color: '#000000'}) //边框
  17. .alignRules({
  18. top: { anchor: '__container__', align: VerticalAlign.Center },
  19. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  20. })
  21. .id('txt1')
  22. }
  23. }
  24. }

2,统一小写

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("ABCD")
  7. .fontWeight(900)//加粗
  8. .textCase(TextCase.LowerCase) //字母小写
  9. .fontSize(30)
  10. .fontColor("#22aaff")
  11. .width(200) //宽度
  12. .height(200) //高度
  13. .backgroundColor("#ccaabb") //背景色
  14. .textAlign(TextAlign.Center)//文字水平居中
  15. .borderRadius(10) //背景圆角
  16. .border({width: 2, color: '#000000'}) //边框
  17. .alignRules({
  18. top: { anchor: '__container__', align: VerticalAlign.Center },
  19. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  20. })
  21. .id('txt1')
  22. }
  23. }
  24. }

3,正常显示

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("ABcd")
  7. .fontWeight(900)//加粗
  8. .textCase(TextCase.Normal) //字母正常显示
  9. .fontSize(30)
  10. .fontColor("#22aaff")
  11. .width(200) //宽度
  12. .height(200) //高度
  13. .backgroundColor("#ccaabb") //背景色
  14. .textAlign(TextAlign.Center)//文字水平居中
  15. .borderRadius(10) //背景圆角
  16. .border({width: 2, color: '#000000'}) //边框
  17. .alignRules({
  18. top: { anchor: '__container__', align: VerticalAlign.Center },
  19. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  20. })
  21. .id('txt1')
  22. }
  23. }
  24. }

十五,设置首行文本缩进

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("ABcdefghfsenfkbkbfgkgldfkledmsam")
  7. .fontWeight(900)//加粗
  8. .textCase(TextCase.Normal) //字母正常显示
  9. .fontSize(30)
  10. .fontColor("#22aaff")
  11. .width(200) //宽度
  12. .height(200) //高度
  13. .backgroundColor("#ccaabb") //背景色
  14. .borderRadius(10) //背景圆角
  15. .border({width: 2, color: '#000000'}) //边框
  16. .textIndent(10)//设置首行文本缩进
  17. .alignRules({
  18. top: { anchor: '__container__', align: VerticalAlign.Center },
  19. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  20. })
  21. .id('txt1')
  22. }
  23. }
  24. }

十六,添加子组件Span

  1. @Entry
  2. @Component
  3. struct TestPage {
  4. build() {
  5. RelativeContainer() {
  6. Text("ABcdefghfsenfkbkbfgkgldfkledmsam"){
  7. Span("我是子组件1")
  8. .fontSize(20)
  9. .fontColor("#000000")
  10. .fontWeight(900)//加粗
  11. Span("我是子组件2")
  12. .fontSize(15)
  13. .fontColor("#ffffff")
  14. Span("我是3")
  15. .fontSize(20)
  16. .fontColor("#ffccdd")
  17. }
  18. .width(300) //宽度
  19. .height(100) //高度
  20. .backgroundColor("#ccaabb") //背景色
  21. .borderRadius(10) //背景圆角
  22. .border({width: 2, color: '#000000'}) //边框
  23. .alignRules({
  24. top: { anchor: '__container__', align: VerticalAlign.Center },
  25. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  26. })
  27. .id('txt1')
  28. }
  29. }
  30. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/828879
推荐阅读
相关标签
  

闽ICP备14008679号