The example code shown here demonstrates how to select a font for
use, instead for example, the default CCoeEnv::NormalFont()
.
Construct a CFont
object.
Set up a font specification (TFontSpec
).
Assign the CFont
to be the nearest available font to
the TFontSpec
suitable for the screen device. The twips size must
be used, because no GetNearestFontInPixels()
member function is
available.
Set the graphic context’s current font to the
CFont
obtained from calling GetNearestFontInTwips()
by calling UseFont()
.
Discard the font using DiscardFont()
.
Release the font by using
CGraphicsDevice::ReleaseFont()
.
// Get an alternative font
_LIT(KMyFontName,"Swiss");
CFont* myFont;
TFontSpec myFontSpec(KMyFontName,1); // to get smallest Swiss font
CGraphicsDevice* screenDevice=iCoeEnv->ScreenDevice();
screenDevice->GetNearestFontInTwips(myFont,myFontSpec);
gc.UseFont(myFont);
// Set the text drawing position & draw
// Note: using the overload of DrawText() that
// draws a background box may give less flicker
TInt fontDescent=myFont->DescentInPixels();
TPoint pos(0,tinyBox.Height()-fontDescent);
pos+=tinyBox.iTl;
_LIT(KMyText,"This text to write");
gc.DrawText(KMyText,pos);
// Discard and destroy the font
gc.DiscardFont();
screenDevice->ReleaseFont(myFont);
attempting to get the smallest available font in a particular style may result in a tiny block font designed for print preview, rather than the smallest in the desired style