QML:自动调整TextInput

时间:2018-03-28 11:00:49

标签: qt fonts qml font-size

Text类型具有此功能fontSizeMode,它会尝试使字体大小适合可视组件的实际大小。因此,当我将字体大小设置为20并调整窗口大小以使此字体太大时,它会自动降低字体大小。

Text {
          text: qsTr("Text")
          font.pointSize: 20
          fontSizeMode: Text.Fit
     }

现在我想将此功能与TextInput类型一起使用,但没有fontSizeMode可用。所以我的问题是,如何使用TextInput实现相同的功能?现在,当设置字体大小并缩小窗口大小时,TextInput中的字符串会被切成两半:

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以缩小元素以将其约束为特定宽度:

  Rectangle {
    width: 100
    height: 50
    color: "red"
    TextInput {
      anchors.centerIn: parent
      font.pointSize: 20
      text: "test"
      scale: Math.min(1, parent.width / contentWidth)
      Rectangle {
        color: "white"
        anchors.fill: parent
        z: -1
      }
    }
  }

答案 1 :(得分:0)

我坚持这个解决方案。将font.pixelSize属性绑定到父高度。虽然我不确定它是否100%正确,但它确实完全符合我的要求。如果有任何问题,请告诉我。

 Text {
      text: qsTr("Text")
      font.pixelSize: 0.1 * parent.height
 }
相关问题