TextMorph example with wrapFlag: true
Last updated at 3:20 pm UTC on 9 January 2022
TextMorph example
Use the default font and fixed width of the TextMorph. Vertical expansion as needed so that the string fits.
Vertical expansion is cause by
textMorph wrapFlag: true.
Try the code snippet with and without this statement.
| textMorph |
textMorph :=TextMorph new
contents: 'a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text '.
textMorph wrapFlag: true.
textMorph width: 400.
textMorph color: Color white.
textMorph backgroundColor: Color blue.
textMorph openInHand
The same as above but with another font:
| fontRef textMorph aString aText |
aString := 'Text: abcde fghij klmno pqrst uvwxy z ... a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text a text '.
fontRef := (TextFontReference toFont: (StrikeFont familyName: 'Atlanta' size: 22)).
aText := aString asText addAttribute: fontRef.
textMorph :=TextMorph new contents: aText.
textMorph wrapFlag: true.
textMorph width: 400.
textMorph color: Color white.
textMorph backgroundColor: Color blue.
textMorph openInHand
Two TextMorph instances with vertical expansion in a container.
container := AlignmentMorph newColumn.
With the method #newColumn an instance of AligmentMorph is obtained where the Example - aMorph addMorph: anotherMorph (no layout and TableLayout) will be stacked vertically.
container hResizing: #shrinkWrap;
vResizing: #shrinkWrap.
These settings make the extent of the container adapt to the size needed by the Example - aMorph addMorph: anotherMorph (no layout and TableLayout):
tm1 : aTextMorph
tm2 : aTextMorph
tm1 (the title morph) is added first.
Then
container addMorphBack: tm2
is used to add the text body after tm1. It #addMorph: would have been used it would have been added in front of tm1.
| container fontRef tm1 tm2 aString aText |
fontRef := (TextFontReference toFont: (StrikeFont familyName: 'Atlanta' size: 22)).
aString := 'a Title (= a short text in instance 1 of TextMorph) '.
tm1 :=TextMorph new contents: (aString asText addAttribute: fontRef).
tm1 backgroundColor: Color white.
tm1 wrapFlag: true.
tm1 width: 400.
aString := 'Text in instance 2 of TextMorph: abcde fghij klmno pqrst uvwxy z ... ABCDE FGHIJ KLMNO PQRST UVWXY Z a text a text a text a text a text a text a text a text a text a text a text a text '.
aText := aString asText addAttribute: fontRef.
tm2 :=TextMorph new contents: aText.
tm2 wrapFlag: true.
tm2 width: 400.
tm2 color: Color white.
tm2 backgroundColor: Color blue.
container := AlignmentMorph newColumn.
container hResizing: #shrinkWrap;
vResizing: #shrinkWrap.
container addMorph: tm1.
container addMorphBack: tm2.
container openInHand

Note
In this case the AlignmentMorph is not really needed. The following gives a similar result
container := Morph new.
container hResizing: #shrinkWrap;
vResizing: #shrinkWrap.
See The AlignmentMorph may be replaced by any Morph class