[Newbies] Centering text in morphic

Johann Hibschman johannh at gmail.com
Wed Feb 8 13:51:55 UTC 2012


How can I center text in morphic? I have a RectangleMorph subclass
representing a "tile." I'd like to display a centered string label
within the morph.

My current approach is to just override #drawOn: with:

    TileMorph>>drawOn: aCanvas
        super drawOn: aCanvas.
        aCanvas drawString: 'hi' on: self innerBounds

However, that just always puts the text in the upper-left corner.
(Code is from memory, so I may have got something wrong.)

By "center", I want the baseline-to-max-ascent for the font to be
centered vertically (not the actual height of whatever characters I
have), while the actual width is centered vertically.

Would it be better to use a StringMorph for placing text like this?
I'm afraid I don't yet understand what layout options I'd need to use
to make it work.

Thanks,
- Johann


More information about the Beginners mailing list