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:

        'DrawBot is great',
        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
    x, y, w, h = (10, 10, 300, 300)
    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)


  • admin

    and the resulting image 🙂


Log in to reply