2017-06-05 51 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

我想出了这个问题。你的建议是对的。谢谢 –