Displaying Text

Displaying text is easy. The class SPTextField should be quite self explanatory. By default, it uses the system fonts of the iPhone.

SPTextField *textField = [SPTextField textFieldWithWidth:145 height:80 
    text:@"Text" fontName:@"Helvetica" fontSize:12.0f color:0xff0000];
textField.hAlign = SPHAlignRight;  // horizontal alignment
textField.vAlign = SPVAlignBottom; // vertical alignment
textField.border = YES;

System Fonts

Here is a list of fonts that are installed per default on any iOS version:

The default iOS fonts

You can include custom fonts with your project by including the UIAppFonts key (an array of strings with the FULL filename including the extension) in your Info.plist as noted here.

Using system fonts like this is adequate for text that does not change very often. However, if your textfield constantly changes its contents, or if you want to display a fancy font that's not available on the iPhone, you should use a bitmap font instead.

Bitmap Fonts

A bitmap font is a texture containing all the characters you want to display. The positions of the characters are defined in an XML document. This is all Sparrow needs to render a text. To create the necessary files, there are several options:

  • Littera a full-featured free online bitmap font generator.
  • The AngelCode Bitmap Font Generator for Windows contains all the basic functionality you need.
  • On OS X, we recommend Glyph Designer – an excellent tool that allows you to add special effects to your fonts, as well.
  • Also exclusive for OS X: bmGlyph, available on the Mac App Store.

Whatever tool you choose: after loading a font in that tool, export the font data as an XML file, and the texture in PNG format with white characters on a transparent background (32 bit). The result is a “.fnt” file and an associated image containing the characters.

To use the bitmap font, just register it at SPTextField:

[SPTextField registerBitmapFontFromFile:@"bmp_font.fnt"];

After registering the font, it can be used just like any system font. Just assign the right “fontName” to your text field (look it up in the “face” attribute of the font XML).

textField.fontName = "bitmap-font-name";

That's all you need to do!

By the way: you can add the font PNG to your texture atlas, as well. Just use the following method for registering the font:

NSString *bmpFontName = [SPTextField registerBitmapFontFromFile:@"bmp_font.fnt"
    texture:[myAtlas textureByName:@"bmp_font"]];

That way, you save texture space and speed up the rendering.

Next section: Pivot Points

  manual/displaying_text.txt · Last modified: 2013/12/13 10:06 by daniel
Powered by DokuWiki