2017-06-05 123 views
1

在研究QML和QtQuick的過程中,出現了以下問題。如何通過減少它所在的元素來使文本自動縮小字體大小。 現在我有這個方法在qml中自動調整文本

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 
     } 
    } 
} 

它的工作原理,但不是太優雅。也許有一些文本自動調整大小的方法。如果包裹在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 
    } 
} 
} 

回答

3

使用文本元素fontSizeMode屬性來設置自動調整大小(Text.HorizontalFitText.VerticalFitText.Fit)。 您可以使用minimumPixelSize屬性調整最小字體大小。

http://doc.qt.io/qt-5/qml-qtquick-text.html#fontSizeMode-prop的細節

+0

我看着這些方法,但由於某些原因,有沒有效果 –

+0

@v_sith_v:您可以編輯您的問題,包括與'fontSizeMode'屬性你試試? – derM

+0

我想出了這個問題。你的建議是對的。謝謝 –