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 [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
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 > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners > _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Wed, Feb 8, 2012 at 12:44 PM, Lawson English <[hidden email]> wrote:
> 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" Thanks, that's helpful. I don't think I'm going to use SimpleButtonMorph, since it builds in a fixed margin, but reading its implementation was useful. It makes a StringMorph and uses its #extent to do the centering, and I can just do the same thing. (And I can just look at StringMorph's #extent if I need to.) If SimpleButtonMorph works by embedding a StringMorph, I think that's a sign that I should do that rather than override #drawOn. Johann _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Sure. My first pass at writing code using Morphic is to see if an
existing class does what I want and then deciding if it is worth the time to "roll my own" using whatever insight I got from seeing how the existing code works. #drawOn should only be used if you need to do things which don't use exisint morphs and/or need a great deal of speed. The built-in composition capabilities of Morphic are usually powerful enough to accomplish any kind of behavior using compositions of simpler morphs. Obviously, you don't create high-level paint programs using morphs as the pixels, but anything higher level than that is probably a good candidate for composition. OTOH, if I was to write an educational program showing how line-drawing algorithms work, I probably WOULD use morphs as pixels. YMMV. L On 2/9/12 6:40 AM, Johann Hibschman wrote: > Thanks, that's helpful. I don't think I'm going to use > SimpleButtonMorph, since it builds in a fixed margin, but reading its > implementation was useful. It makes a StringMorph and uses its #extent > to do the centering, and I can just do the same thing. (And I can just > look at StringMorph's #extent if I need to.) If SimpleButtonMorph > works by embedding a StringMorph, I think that's a sign that I should > do that rather than override #drawOn. Johann > _______________________________________________ Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |