在学习QML和QtQuick的过程中,出现了如下问题。如何通过减少文本所在的元素使文本自动减小字体大小。现在我有了这个方法
Rectangle {
id: main_window
width: 700
height: 500
property int main_w: main_window.width
Rectangle {
width: 400
height: 400
anchors.centerIn: parent
color: 'green'
Text {
text: "SIZE ME!!!"
anchors.centerIn: parent
color: 'white'
font.pointSize: {
if (main_window.main_w < main_window.width)
return main_window.main_w / 35 // we need 20pt
return main_window.width / 35
}
visible: {
if (parent.width < 100)
return false
return true
}
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但不太优雅。也许有一些方法可以自动调整文本大小。如果包装在ColumnLayout不起作用。
请帮忙。谢谢
这是我fontSizeMode尝试的代码:
Rectangle {
id: root
width: 700
height: 700
property int mrg: 10
Rectangle {
anchors.centerIn: parent
width: 400
height: 400
color: 'green'
Text {
id: field
text: "Size me!"
minimumPointSize: 10
font.pointSize: 60
fontSizeMode: Text.Fit
color: 'white'
anchors.centerIn: parent
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
使用 Text 元素fontSizeMode属性设置自动调整大小(Text.HorizontalFit、Text.VerticalFit或Text.Fit)。您可以使用minimumPixelSize属性调整最小字体大小。
见http://doc.qt.io/qt-5/qml-qtquick-text.html#fontSizeMode-prop详细信息
| 归档时间: |
|
| 查看次数: |
11305 次 |
| 最近记录: |