SPTextField Class Reference
| Inherits from | SPDisplayObjectContainer : SPDisplayObject : SPEventDispatcher : NSObject |
| Declared in | SPTextField.h |
Overview
An SPTextField displays text, either using standard iOS fonts or a custom bitmap font.
You can set all properties you are used to, like the font name and size, a color, the horizontal and vertical alignment, etc. The border property is helpful during development, because it lets you see the bounds of the textfield.
There are two types of fonts that can be displayed:
- Standard iOS fonts. This renders the text with standard iOS fonts like Verdana or Arial. Use this method if you want to keep it simple, and if the text changes not too often. Simply pass the font name to the corresponding property.
- Bitmap fonts. If you need speed or fancy font effects, use a bitmap font instead. That is a
font that has its glyphs rendered to a texture atlas. To use it, first register the font with
the method
registerBitmapFont:, and then pass the font name to the corresponding property of the text field.
For the latter, we recommend one of the following tools; both support Sparrow natively.
- Glyph Designer from 71squared.
- bmGlyph available in the App Store
Alternatively, you can use the Bitmap Font Generator from Angel Code, which is a free tool for Windows. Export the font data as an XML file and the texture as a png with white characters on a transparent background (32 bit).
Here is a sample with a standard font:
SPTextField *textField = [SPTextField textFieldWithWidth:300 height:100 text:@"Hello world!"];
textField.hAlign = SPHAlignCenter;
textField.vAlign = SPVAlignCenter;
textField.fontSize = 18;
textField.fontName = @"Georgia-Bold";
And now we use a bitmap font:
// Register the font; the returned font name is the one that is defined in the font XML.
NSString *fontName = [SPTextField registerBitmapFontFromFile:@"bitmap_font.fnt"];
SPTextField *textField = [SPTextField textFieldWithWidth:300 height:100 text:@"Hello world!"];
textField.fontName = fontName;
Tip: Sparrow comes with a small bitmap font that is great for debug output. Just assign the
font name SPBitmapFontMiniName to a text field to use it.
Tasks
Initialization
-
– initWithWidth:height:text:fontName:fontSize:color:Initialize a text field with all important font properties. Designated Initializer.
-
– initWithWidth:height:text:Initialize a text field with default settings (Helvetica, 14pt, black).
-
– initWithWidth:height:Initialize a text field with default settings (Helvetica, 14pt, black) and an empty string.
-
– initWithText:Initialize a 128x128 textField (Helvetica, 14pt, black).
-
+ textFieldWithWidth:height:text:fontName:fontSize:color:Factory method.
-
+ textFieldWithWidth:height:text:Factory method.
-
+ textFieldWithText:Factory method.
Methods
-
+ registerBitmapFont:Makes a bitmap font available at any text field, using texture and name as defined in the file.
-
+ registerBitmapFont:name:Makes a bitmap font available at any text field, using the texture defined in the file and manually providing the font name.
-
+ registerBitmapFontFromFile:Makes a bitmap font available at any text field, using texture and name as defined in the file.
-
+ registerBitmapFontFromFile:texture:Makes a bitmap font available at any text field, using a custom texture.
-
+ registerBitmapFontFromFile:texture:name:Makes a bitmap font available at any text field, using a custom texture and font name.
-
+ unregisterBitmapFont:Unregisters the bitmap font of this name.
-
+ registeredBitmapFont:Get the bitmap font that was registered under a certain name.
Properties
-
textThe displayed text.
property -
fontNameThe name of the font.
property -
fontSizeThe size of the font. For bitmap fonts, use
propertySPNativeFontSizefor the original size. -
hAlignThe horizontal alignment of the text.
property -
vAlignThe vertical alignment of the text.
property -
borderAllows displaying a border around the edges of the text field. Useful for visual debugging.
property -
colorThe color of the text.
property -
textBoundsThe bounds of the actual characters inside the text field.
property -
kerningAllows using kerning information with a bitmap font (where available). Default is YES.
property -
autoScaleIndicates whether the font size is scaled down so that the complete text fits into the text field. Default is NO.
property
Properties
autoScale
Indicates whether the font size is scaled down so that the complete text fits into the text field. Default is NO.
@property (nonatomic, assign) BOOL autoScaleDiscussion
Indicates whether the font size is scaled down so that the complete text fits into the text field. Default is NO.
Declared In
SPTextField.hborder
Allows displaying a border around the edges of the text field. Useful for visual debugging.
@property (nonatomic, assign) BOOL borderDiscussion
Allows displaying a border around the edges of the text field. Useful for visual debugging.
Declared In
SPTextField.hcolor
The color of the text.
@property (nonatomic, assign) uint colorDiscussion
The color of the text.
Declared In
SPTextField.hfontName
The name of the font.
@property (nonatomic, copy) NSString *fontNameDiscussion
The name of the font.
Declared In
SPTextField.hfontSize
The size of the font. For bitmap fonts, use SPNativeFontSize for the original size.
@property (nonatomic, assign) float fontSizeDiscussion
The size of the font. For bitmap fonts, use SPNativeFontSize for the original size.
Declared In
SPTextField.hhAlign
The horizontal alignment of the text.
@property (nonatomic, assign) SPHAlign hAlignDiscussion
The horizontal alignment of the text.
Declared In
SPTextField.hkerning
Allows using kerning information with a bitmap font (where available). Default is YES.
@property (nonatomic, assign) BOOL kerningDiscussion
Allows using kerning information with a bitmap font (where available). Default is YES.
Declared In
SPTextField.htext
The displayed text.
@property (nonatomic, copy) NSString *textDiscussion
The displayed text.
Declared In
SPTextField.hClass Methods
registerBitmapFont:
Makes a bitmap font available at any text field, using texture and name as defined in the file.
+ (NSString *)registerBitmapFont:(SPBitmapFont *)fontReturn Value
The name of the font as defined in the font XML.
Discussion
Makes a bitmap font available at any text field, using texture and name as defined in the file.
Declared In
SPTextField.hregisterBitmapFont:name:
Makes a bitmap font available at any text field, using the texture defined in the file and manually providing the font name.
+ (NSString *)registerBitmapFont:(SPBitmapFont *)font name:(NSString *)fontNameReturn Value
The name of the font that was passed to the method.
Discussion
Makes a bitmap font available at any text field, using the texture defined in the file and manually providing the font name.
Declared In
SPTextField.hregisterBitmapFontFromFile:
Makes a bitmap font available at any text field, using texture and name as defined in the file.
+ (NSString *)registerBitmapFontFromFile:(NSString *)pathReturn Value
The name of the font as defined in the font XML.
Discussion
Makes a bitmap font available at any text field, using texture and name as defined in the file.
Declared In
SPTextField.hregisterBitmapFontFromFile:texture:
Makes a bitmap font available at any text field, using a custom texture.
+ (NSString *)registerBitmapFontFromFile:(NSString *)path texture:(SPTexture *)textureReturn Value
The name of the font as defined in the font XML.
Discussion
Makes a bitmap font available at any text field, using a custom texture.
Declared In
SPTextField.hregisterBitmapFontFromFile:texture:name:
Makes a bitmap font available at any text field, using a custom texture and font name.
+ (NSString *)registerBitmapFontFromFile:(NSString *)path texture:(SPTexture *)texture name:(NSString *)fontNameDiscussion
Makes a bitmap font available at any text field, using a custom texture and font name.
@retrurn The name of the font that was passed to the method.
Declared In
SPTextField.hregisteredBitmapFont:
Get the bitmap font that was registered under a certain name.
+ (SPBitmapFont *)registeredBitmapFont:(NSString *)nameDiscussion
Get the bitmap font that was registered under a certain name.
Declared In
SPTextField.htextFieldWithText:
Factory method.
+ (instancetype)textFieldWithText:(NSString *)textDiscussion
Factory method.
Declared In
SPTextField.htextFieldWithWidth:height:text:
Factory method.
+ (instancetype)textFieldWithWidth:(float)width height:(float)height text:(NSString *)textDiscussion
Factory method.
Declared In
SPTextField.hInstance Methods
initWithText:
Initialize a 128x128 textField (Helvetica, 14pt, black).
- (instancetype)initWithText:(NSString *)textDiscussion
Initialize a 128x128 textField (Helvetica, 14pt, black).
Declared In
SPTextField.hinitWithWidth:height:
Initialize a text field with default settings (Helvetica, 14pt, black) and an empty string.
- (instancetype)initWithWidth:(float)width height:(float)heightDiscussion
Initialize a text field with default settings (Helvetica, 14pt, black) and an empty string.
Declared In
SPTextField.hinitWithWidth:height:text:
Initialize a text field with default settings (Helvetica, 14pt, black).
- (instancetype)initWithWidth:(float)width height:(float)height text:(NSString *)textDiscussion
Initialize a text field with default settings (Helvetica, 14pt, black).
Declared In
SPTextField.hinitWithWidth:height:text:fontName:fontSize:color:
Initialize a text field with all important font properties. Designated Initializer.
- (instancetype)initWithWidth:(float)width height:(float)height text:(NSString *)text fontName:(NSString *)name fontSize:(float)size color:(uint)colorDiscussion
Initialize a text field with all important font properties. Designated Initializer.
Declared In
SPTextField.h