Squeak
  links to this page:    
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
How to use TextMorph with multiple fonts
Last updated at 1:41 pm UTC on 16 May 2017
How to use TextMorph with multiple fonts
Question Ducasse February 20, 2004: I would like to have a textMorph (if you have soemthing better please tell me) that contains different fonts. I tried
  TextMorph new openInWorld;  string: 'JJLLJL' 
         fontName: #Atlanta size: 22 wrap: false
It works. What I do not succeed to do is to add another text with another font. tried
	fontName: #Atlanta size: 18.	
	contents: 'stéphane in accuate18'

Answer Ken Causey Near the bottom of the class comment for TextMorph it notes that a TextMorph text can only contain a single font. I think you might have more luck with PluggableTextMorph. Alternatively just have a separate morph for each font.

Ducasse: Thanks Ken. But when I have a TextMorph I can select part of it and change the fonts. So this should be possible. I cannot believe this is not possible.

By the way the interfaces and relationships between strikeFont and TextStyle are really not well defined. It would be good that somebody understanding that propose some clean up.

Ken Causey You are right this is much easier to use Text
 	| text size appendText|
	text :=  Text string: 'This is a text' 
                attributes: { (TextColor red) . 
                              (TextFontReference toFont:
	                       ((TextStyle named: #Accuat) fontNamed: 'Accuat18'))
                            }.

	size := text size.
	appendText := 'stephane will survive'.
	text addAttribute: (TextFontReference toFont:
	                      ((TextStyle named: #Accuve) fontNamed: 'Accuve12')) 
            from: size + 1 to: size + appendText size.

	text append: appendText.
	^ text

I should really write a test so that others do not spend hours on this like me.
Ducasse: Thanks a lot. I saw that I had to go directly to the text object that's why I abandoned the textMorph. I was fighting with the TextStyle interface but in fact going from the StrikeFont one is indeed much better.
See also TextStream.

Answer Steven Swerling: You can change fonts w/in the Text object itself using TextAttributes. Try this:
 | font1 font2 t1 t2 |
tMorph _ TextMorph new.
font1 _ (TextFontReference toFont: 
         (StrikeFont familyName: 'Atlanta' size: 22)).
font2 _ (TextFontReference toFont:
         (StrikeFont familyName: 'Atlanta' size: 11)).
t1 _ 'this is font1' asText addAttribute: font1.
t2 _ ' and this is font2' asText addAttribute: font2.
tMorph contents: (t1,t2).
tMorph openInHand.

Answer Boris Gaertner Yes, but [multiple fonts] requires some preparations. To see all fonts, evaluate this statement: Font survey