属性

  1. advance : size
  2. antialiasing : bool
  3. baseUrl : url
  4. clip : bool
  5. color : color
  6. //字符内容宽高度
  7. contentHeight : real
  8. contentWidth : real
  9. effectiveHorizontalAlignment : enumeration
  10. //字符超过显示长度省略显示
  11. elide : enumeration
  12. Text.ElideNone - the default
  13. Text.ElideLeft
  14. Text.ElideMiddle
  15. Text.ElideRight
  16. //字体参数的设置
  17. font.bold : bool 粗体
  18. font.capitalization : enumeration
  19. font.family : string 字体类型
  20. font.hintingPreference : enumeration
  21. font.italic : bool 斜体
  22. font.kerning : bool
  23. font.letterSpacing : real 字母与字母之间的距离
  24. font.pixelSize : int 字体大小 像素做单位
  25. font.pointSize : real 字体大小 磅做单位
  26. font.preferShaping : bool
  27. font.strikeout : bool
  28. font.styleName : string
  29. font.underline : bool 下划线
  30. font.weight : enumeration
  31. font.wordSpacing : real
  32. fontInfo.bold : bool
  33. fontInfo.family : string
  34. fontInfo.italic : bool
  35. fontInfo.pixelSize : string
  36. fontInfo.pointSize : real
  37. fontInfo.styleName : string
  38. fontInfo.weight : int
  39. fontSizeMode : enumeration
  40. horizontalAlignment : enumeration
  41. hoveredLink : string
  42. lineCount : int 文本行数
  43. lineHeight : real 每行间隔高度
  44. lineHeightMode : enumeration
  45. linkColor : color
  46. maximumLineCount : int
  47. minimumPixelSize : int
  48. minimumPointSize : int
  49. renderType : enumeration
  50. style : enumeration
  51. styleColor : color
  52. text : string
  53. textFormat : enumeration
  54. //字体距离边框顶部距离
  55. topPadding : real
  56. padding : real
  57. //体距离边框左侧距离
  58. leftPadding : real
  59. //体距离边框底部距离
  60. bottomPadding : real
  61. //体距离边框右侧距离
  62. rightPadding : real
  63. truncated : bool
  64. verticalAlignment : enumeration
  65. wrapMode : enumeration //换行方式 文本换行Text.WordWrap

信号

  1. lineLaidOut(object line)
  2. linkActivated(string link)
  3. linkHovered(string link)

方法

  1. forceLayout()
  2. linkAt(real x, real y)

显示文本你需要使用Text元素(Text Element)。它最值得注意的属性时字符串类型的text属性。这个元素会使用给出的text(文本)与font(字体)来计算初始化的宽度与高度。可以使用字体属性组来(font property group)来改变当前的字体,例如font.family,font.pixelSize,等等。改变文本的颜色值只需要改变颜色属性就可以了。

样式

  1. Column {
  2. Rectangle{
  3. width: 100
  4. height: txt.contentHeight
  5. opacity: 0.5
  6. color: "blue"
  7. Text {
  8. id: txt
  9. anchors.fill: parent
  10. text: "Hello World! 152285242151"
  11. font.family: "Helvetica"
  12. font.pointSize: 20
  13. elide: Text.ElideRight
  14. color: "red"
  15. leftPadding: 0
  16. // padding: 20
  17. // topPadding: 20
  18. // bottomPadding: 20
  19. }
  20. }
  21. Text { font.pointSize: 24; text: "Normal" }
  22. Text { font.pointSize: 24; text: "Raised"; style: Text.Raised; styleColor: "#AAAAAA" }
  23. Text { font.pointSize: 24; text: "Outline";style: Text.Outline; styleColor: "red" }
  24. Text { font.pointSize: 24; text: "Sunken"; style: Text.Sunken; styleColor: "#AAAAAA" }
  25. } Text {
  26. text: "The quick brown fox"
  27. color: "#303030"
  28. font.family: "Ubuntu"
  29. font.pixelSize: 28
  30. }

image.png
文本可以使用horizontalAlignment与verticalAlignment属性来设置它的对齐效果。为了提高文本的渲染效果,你可以使用style和styleColor属性来配置文字的外框效果,浮雕效果或者凹陷效果。对于过长的文本,你可能需要使用省略号来表示,例如A very … long text,你可以使用elide属性来完成这个操作。elide属性允许你设置文本左边,右边或者中间的省略位置。如果你不想’….’省略号出现,并且希望使用文字换行的方式显示所有的文本,你可以使用wrapMode属性(这个属性只在明确设置了宽度后才生效):

  1. Text {
  2. width: 40; height: 120
  3. text: 'A very long text'
  4. // '...' shall appear in the middle
  5. elide: Text.ElideMiddle //省略。。。替换过长文本
  6. // red sunken text styling
  7. style: Text.Sunken
  8. styleColor: '#FF4444'
  9. // align text to the top
  10. verticalAlignment: Text.AlignTop
  11. // only sensible when no elide mode
  12. // wrapMode: Text.WordWrap //换行
  13. }

一个text元素(text element)只显示的文本,它不会渲染任何背景修饰。除了显示的文本,text元素背景是透明的。为一个文本元素提供背景是你自己需要考虑的问题。

文本内容格式的支持

  1. Column {
  2. Text {
  3. font.pointSize: 24
  4. text: "<b>Hello</b> <i>World!</i>"
  5. }
  6. Text {
  7. font.pointSize: 24
  8. textFormat: Text.RichText //富文本
  9. text: "<b>Hello</b> <i>World!</i>"
  10. }
  11. Text {
  12. font.pointSize: 24
  13. textFormat: Text.PlainText //纯文本
  14. text: "<b>Hello</b> <i>World!</i>"
  15. }
  16. Text {
  17. font.pointSize: 24
  18. textFormat: Text.MarkdownText
  19. text: "**Hello** *World!*"
  20. }
  21. }

image.png
注意
知道一个文本元素(Text Element)的初始宽度与高度是依赖于文本字符串和设置的字体这一点很重要。一个没有设置宽度或者文本的文本元素(Text Element)将不可见,默认的初始宽度是0。

显示超链接:

  1. Rectangle{
  2. id: rect
  3. width: 200
  4. height: 50
  5. border.color: "black"
  6. Text {
  7. // textFormat: Text.RichText
  8. text: "See the <a href=\"http://qt-project.org\">Qt Project website</a>."
  9. MouseArea{
  10. anchors.fill: parent
  11. hoverEnabled: true
  12. cursorShape: Qt.PointingHandCursor //修改鼠标移动到文本上是图案
  13. onClicked: {
  14. //鼠标点击需要做的操作
  15. }
  16. }
  17. onLinkActivated: { //点击链接触发
  18. console.log(link + " link activated")
  19. }
  20. onLinkHovered: { //鼠标移动到文本上触发
  21. console.log("hover ",link)
  22. }
  23. onHoveredLinkChanged: {
  24. console.log("hover link changed: ",link)
  25. }
  26. }
  27. }

image.png
注意
通常你想要对文本元素布局时,你需要区分文本在文本元素内部的边界对齐和由元素边界自动对齐。前一种情况你需要使用horizontalAlignment和verticalAlignment属性来完成,后一种情况你需要操作元素的几何形状或者使用anchors(锚定)来完成。