Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



How to select a font

The example code shown here demonstrates how to select a font for use, instead for example, the default CCoeEnv::NormalFont().

  1. Construct a CFont object.

  2. Set up a font specification (TFontSpec).

  3. 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.

  4. Set the graphic context’s current font to the CFont obtained from calling GetNearestFontInTwips() by calling UseFont().

  5. Discard the font using DiscardFont().

  6. 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);


Notes