Text height



  • How to make so that the height of the text has been equal to the height of a rectangle in which the text is?


  • Moderators

    Hi!
    The following should work:

        Rectangle {
            width: 300
            height: 100
    
            Text {
                text: "Hallo!"
                font.pixelSize: parent.height
                anchors.centerIn: parent
            }
        }
    


  • obtained the margin on the top and bottom of the text
    That is, the height of the text < the height of the rectangle

    image


  • Moderators

    This is a bit better as it seems to use contentHeight internally:

        Rectangle {
            width: 300
            height: 100
            anchors.centerIn: parent
    
            Text {
                text: "Äp!"
                anchors.fill: parent
                fontSizeMode: Text.VerticalFit
                font.pixelSize: 1000
            }
        }
    

    But still "Hallo!" is not fully scaled up. I think the problem stems from the fact that contentHeight doesn't return the actual bounding box of the rendered text but the sum of the cap height and descender height.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.