属性
advance : size
antialiasing : bool
baseUrl : url
clip : bool
color : color
//字符内容宽高度
contentHeight : real
contentWidth : real
effectiveHorizontalAlignment : enumeration
//字符超过显示长度省略显示
elide : enumeration
Text.ElideNone - the default
Text.ElideLeft
Text.ElideMiddle
Text.ElideRight
//字体参数的设置
font.bold : bool 粗体
font.capitalization : enumeration
font.family : string 字体类型
font.hintingPreference : enumeration
font.italic : bool 斜体
font.kerning : bool
font.letterSpacing : real 字母与字母之间的距离
font.pixelSize : int 字体大小 像素做单位
font.pointSize : real 字体大小 磅做单位
font.preferShaping : bool
font.strikeout : bool
font.styleName : string
font.underline : bool 下划线
font.weight : enumeration
font.wordSpacing : real
fontInfo.bold : bool
fontInfo.family : string
fontInfo.italic : bool
fontInfo.pixelSize : string
fontInfo.pointSize : real
fontInfo.styleName : string
fontInfo.weight : int
fontSizeMode : enumeration
horizontalAlignment : enumeration
hoveredLink : string
lineCount : int 文本行数
lineHeight : real 每行间隔高度
lineHeightMode : enumeration
linkColor : color
maximumLineCount : int
minimumPixelSize : int
minimumPointSize : int
renderType : enumeration
style : enumeration
styleColor : color
text : string
textFormat : enumeration
//字体距离边框顶部距离
topPadding : real
padding : real
//体距离边框左侧距离
leftPadding : real
//体距离边框底部距离
bottomPadding : real
//体距离边框右侧距离
rightPadding : real
truncated : bool
verticalAlignment : enumeration
wrapMode : enumeration //换行方式 文本换行Text.WordWrap
信号
lineLaidOut(object line)
linkActivated(string link)
linkHovered(string link)
方法
forceLayout()
linkAt(real x, real y)
显示文本你需要使用Text元素(Text Element)。它最值得注意的属性时字符串类型的text属性。这个元素会使用给出的text(文本)与font(字体)来计算初始化的宽度与高度。可以使用字体属性组来(font property group)来改变当前的字体,例如font.family,font.pixelSize,等等。改变文本的颜色值只需要改变颜色属性就可以了。
样式
Column {
Rectangle{
width: 100
height: txt.contentHeight
opacity: 0.5
color: "blue"
Text {
id: txt
anchors.fill: parent
text: "Hello World! 152285242151"
font.family: "Helvetica"
font.pointSize: 20
elide: Text.ElideRight
color: "red"
leftPadding: 0
// padding: 20
// topPadding: 20
// bottomPadding: 20
}
}
Text { font.pointSize: 24; text: "Normal" }
Text { font.pointSize: 24; text: "Raised"; style: Text.Raised; styleColor: "#AAAAAA" }
Text { font.pointSize: 24; text: "Outline";style: Text.Outline; styleColor: "red" }
Text { font.pointSize: 24; text: "Sunken"; style: Text.Sunken; styleColor: "#AAAAAA" }
} Text {
text: "The quick brown fox"
color: "#303030"
font.family: "Ubuntu"
font.pixelSize: 28
}
文本可以使用horizontalAlignment与verticalAlignment属性来设置它的对齐效果。为了提高文本的渲染效果,你可以使用style和styleColor属性来配置文字的外框效果,浮雕效果或者凹陷效果。对于过长的文本,你可能需要使用省略号来表示,例如A very … long text,你可以使用elide属性来完成这个操作。elide属性允许你设置文本左边,右边或者中间的省略位置。如果你不想’….’省略号出现,并且希望使用文字换行的方式显示所有的文本,你可以使用wrapMode属性(这个属性只在明确设置了宽度后才生效):
Text {
width: 40; height: 120
text: 'A very long text'
// '...' shall appear in the middle
elide: Text.ElideMiddle //省略。。。替换过长文本
// red sunken text styling
style: Text.Sunken
styleColor: '#FF4444'
// align text to the top
verticalAlignment: Text.AlignTop
// only sensible when no elide mode
// wrapMode: Text.WordWrap //换行
}
一个text元素(text element)只显示的文本,它不会渲染任何背景修饰。除了显示的文本,text元素背景是透明的。为一个文本元素提供背景是你自己需要考虑的问题。
文本内容格式的支持
Column {
Text {
font.pointSize: 24
text: "<b>Hello</b> <i>World!</i>"
}
Text {
font.pointSize: 24
textFormat: Text.RichText //富文本
text: "<b>Hello</b> <i>World!</i>"
}
Text {
font.pointSize: 24
textFormat: Text.PlainText //纯文本
text: "<b>Hello</b> <i>World!</i>"
}
Text {
font.pointSize: 24
textFormat: Text.MarkdownText
text: "**Hello** *World!*"
}
}
注意
知道一个文本元素(Text Element)的初始宽度与高度是依赖于文本字符串和设置的字体这一点很重要。一个没有设置宽度或者文本的文本元素(Text Element)将不可见,默认的初始宽度是0。
显示超链接:
Rectangle{
id: rect
width: 200
height: 50
border.color: "black"
Text {
// textFormat: Text.RichText
text: "See the <a href=\"http://qt-project.org\">Qt Project website</a>."
MouseArea{
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor //修改鼠标移动到文本上是图案
onClicked: {
//鼠标点击需要做的操作
}
}
onLinkActivated: { //点击链接触发
console.log(link + " link activated")
}
onLinkHovered: { //鼠标移动到文本上触发
console.log("hover ",link)
}
onHoveredLinkChanged: {
console.log("hover link changed: ",link)
}
}
}
注意
通常你想要对文本元素布局时,你需要区分文本在文本元素内部的边界对齐和由元素边界自动对齐。前一种情况你需要使用horizontalAlignment和verticalAlignment属性来完成,后一种情况你需要操作元素的几何形状或者使用anchors(锚定)来完成。