Report abuse

UILabelCustom.h

#import <UIKit/UIKit.h>

@interface UILabelCustom : UILabel {

	CGFontRef customFont;
	int customFontMagicNumber;
}

-(void) loadCustomFont:(NSString *)font Type:(NSString *)type MagicNumber:(int)number;

@end

UILabelCustom.m

#import "UILabelCustom.h"

@implementation UILabelCustom

- (void) loadCustomFont:(NSString *)font Type:(NSString *)type MagicNumber:(int)number {
	// Get the path to our custom font and create a data provider.
	NSString *fontPath = [[NSBundle mainBundle] pathForResource:font ofType:type]; 
	CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
	
	// Create the font with the data provider, then release the data provider.
	customFont = CGFontCreateWithDataProvider(fontDataProvider);
	CGDataProviderRelease(fontDataProvider); 
	
	customFontMagicNumber = number;
	
	// Create the matrix to flip any text drawn to a readable orientation.
	CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
	self.transform = textTransform;
	
}

- (void)drawRect:(CGRect)rect {
	
	// Get the context.
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	// Set the customFont to be the font used to draw.
	CGContextSetFont(context, customFont);
	
	// Set how the context draws the font, what color, how big.
	CGContextSetTextDrawingMode(context, kCGTextFillStroke);
	CGContextSetRGBFillColor(context, 254.0/255.0, 245.0/255.0, 111.0/255.0, 1.0);
	CGContextSetRGBStrokeColor(context, 193.0/255.0, 154.0/255.0, 0.0/255.0, 1.0);
	CGContextSetFontSize(context, rect.size.height-1);  // tweak this
	CGContextSetCharacterSpacing(context,3.0);    // tweak this
	CGContextSetShadow(context,CGSizeMake(4,4),3.5);  // tweak this
	
	// Create an array of Glyph's the size of text that will be drawn.
	int textLength = [self.text length];
	CGGlyph textToPrint[textLength];
	
	// Loop through the entire length of the text.
	for (int i = 0; i < textLength; ++i) {
		// Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
		textToPrint[i] = [self.text characterAtIndex:i] - customFontMagicNumber;	// Default = 23;
	}
	
	// Perform a rough centering of the text... should calculate the glyph bounding boxes but...
	CGContextShowGlyphsAtPoint (context,x,8,textToPrint,textLength);
}
	
-(void)dealloc {
		[super dealloc];
	}
	
@end

Usage

	UILabelCustom *myLabel = [[UILabelCustom alloc] initWithFrame:CGRectMake(0, 0, 94, 25)];
	[myLabel loadCustomFont:@"MYCUSTOMTTF" Type:@"TTF" MagicNumber:38];  // Include the TTF in your bundle
	myLabel.text = [player uppercaseString];
	myLabel.center = playerLabelCenterPoint;
	myLabel.backgroundColor = [UIColor clearColor];
	[self myLabel];
	[myLabel release];