Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


How to select a font

The example code shown here demonstrates how to select a font for use, instead of 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 GetNearestFontToMaxHeightInPixels() member function is available.

  4. Set the graphic context’s current font to the CFont obtained from calling GetNearestFontToMaxHeightInTwips() 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->GetNearestFontToMaxHeightInTwips(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->FontMaxDescent();
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);

[Top]


Notes