Vertical Align



  • I'm looking for a property to work with textBox() to align the text to the vertical center. What I'm imagining is something like this:

    textBox(
        'DrawBot is great',
        (
            width()*0.2,
            height()*0.2,
            width()*0.6,
            height()*0.6
        ),
        align = 'center',
        verticalAlign = 'center'
    )
    

    This way you can come up with a random playing with that verticalAlign or something like that 😊



  • It would be perfect if it was possible to set the vertical alignment directly by some parameter in the textBox object, but it is also possible to calculate it from the height of the text block. If the block height is not known, it is possible to calculate it if the total number of lines is known.

    I couldn't identify a simple way to get neither the size of the text block nor the number of lines that the text occupies inside the textBox object (the textSize() function and the .size() method return a height relative to a unit of the lineHeight).

    Any suggestions?


  • admin

    with textSize you can set a limit on the width or on the height.

    t = """foo
    bar a super long line that will have a line break
    more
    text"""
    
    
    x, y, w, h = (10, 10, 300, 300)
    
    
    fontSize(30)
    tw, th = textSize(t, width=w, align="center")
    
    # move the box down by half 
    # of the leftover whitespace
    textBox(t, (x, y - (h-th)/2, w, h), align="center")
    
    fill(0, 0, 1, .3)
    rect(x, y, w, h)
    

    see: https://drawbot.com/content/text/drawingText.html#drawBot.textSize


  • admin

    and the resulting image 🙂

    441e28b9-fd54-4f58-b72e-767fea7e2e88-image.png


Log in to reply