Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
TextMorph
Last updated at 6:07 pm UTC on 3 November 2006
From the class comment: TextMorphs support
Late in life, TextMorph was made a subclass of BorderedMorph to provide border and background color if desired. In order to keep things compatible, protocols have been redirected so that color (preferably textColor) relates to the text, and backgroundColor relates to the inner fill color.

Text display is clipped to the innerBounds of the rectangle, and text composition is normally performed within a rectangle which is innerBounds inset by the margins parameter.

If text has been embedded in another object, one can elect to fill the owner's shape, in which case the text will be laid out in the shape of the owner's shadow image (including any submorphs other than the text). One can also elect to have the text avoid occlusions, in which case it will avoid the bounds of any sibling morphs that appear in front of it. It may be necessary to update bounds in order for the text runaround to notice the presence of a new occluding shape.

The optional autoFitContents property enables the following feature: if the text contents changes, then the bounds of the morph will be adjusted to fit the minimum rectangle that encloses the text (plus any margins specified). Similarly, any attempt to change the size of the morph will be resisted if this parameter is set. Except...

If the wrapFlag parameter is true, then text will be wrapped at word boundaries based on the composition width (innerBounds insetBy: margins) width.
Asking if the TextMorphs wrapFlag is set
 isWrapped
Setting the wrapFlag
 wrapFlag: true
Assigning content to the TextMorph while at the same time setting the wrap flag:
 contentsWrapped: aTextOrString
Thus an attempt to resize the morph in autofit mode, if it changes the width, will cause the text to be recomposed with the new width, and then the bounds will be reset to the minimum enclosing rectangle. Similarly, if the text contents are changed with the wrapFlag set to true, word wrap will be performed based on the current compostion width, after which the bounds will be set (or not), based on the autoFitcontents property.

Instance variables

 | t |
 t := TextMorph new contents: 'Very first line'; wrapFlag: true.
 t openInWorld.
 2 to: 20 do: [ :i | t contents: (t contents, String cr, ' line ',
  i asString) ]

aTextMorph beAllFont: aFont example
(TextMorph new 
   beAllFont: ((TextStyle default fontOfSize: 36) emphasized: 1);
   color: Color magenta;
   contents: 'Some text') openInWorld

Question: TextMorph abandons 'Principle of Least Astonishment' I don't know if this is considered a bug. but..
(TextMorph new string: 'Text me!' fontName: 'ComicSansMS' size: 26)openInWorld.
shows the proper font but has font size 11.

Answer: Ned Konz November 02, 2004 Don't confuse 'size' with 'pointSize'. The font operations that have 'size' in their names refer to nominal height in pixels. And since a point is not the same as a pixel, these differ by a factor of 96/72 (by default). I picked a nominal factor of 96 dpi because it was close to the actual resolution of today's display devices. Plus,
 fontOfSize: whatever
will find the closest pixel size to whatever.So if you ask for a 26 pixel tall font (which is what you did), this is going to find the closest available font size to 26 pixels/19.5 points. Which apparently was the 11 point font you found.

Question:I just wondered if you'd worked out a way to pop it up in italic?
Answer: Ned Konz 21 December 2001

TextMorph new
contents: (Text string: 'this is something longer than normal' attribute: TextEmphasis italic);
fontName: #ComicBold size: 24;
releaseCachedState; "—-added"
openInWorld


See also: PluggableTextMorph, How to use TextMorph with multiple fonts, How to work with multiple text morphs