[Newbies] Centering text in morphic

Lawson English lenglish5 at cox.net
Wed Feb 8 18:44:37 UTC 2012

Hi Johann, just after you left IRC, I figured out an easy (though 
perhaps not the best) way to do what you want:

a := SimpleButtonMorph new openInWorld.
a actWhen: #buttonDown.   "turns off visible button behavior"
a label: 'new label'.              "lets you change the label to a 
different string of text"
a clipSubmorphs: true.        "true means that your text doesn't extend 
beyond the border of the button"

Hope this helps.

Lawson (saijanai_ on irc)

On 2/8/12 6:51 AM, Johann Hibschman wrote:
> 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
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

More information about the Beginners mailing list