Сначала загрузите шрифт, как показано ниже:
(void)loadFont{ // Get the path to our custom font and create a data provider.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"mycustomfont" ofType:@"ttf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
// Create the font with the data provider, then release the data provider. customFont =
CGFontCreateWithDataProvider(fontDataProvider); CGDataProviderRelease(fontDataProvider);
}
Затем используйте их, как показано ниже:
-(void)drawRect:(CGRect)rect{
[super drawRect:rect]; // Get the context.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect); // 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); CGContextSetFillColorWithColor(context, self.fontColor.CGColor); UIColor * strokeColor = [UIColor blackColor];
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor); CGContextSetFontSize(context, 48.0f);
// Create an array of Glyph's the size of text that will be drawn.
CGGlyph textToPrint[[self.theText length]];
// Loop through the entire length of the text.
for (int i = 0; i < [self.theText length]; ++i) { // Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
textToPrint[i] = [[self.theText uppercaseString] characterAtIndex:i] + 3 - 32;
}
CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, textTransform);
CGContextShowGlyphsAtPoint(context, 20, 50, textToPrint, [self.theText length]);
}
В некоторых случаях вы можете не использовать некоторые загруженные из Интернеташрифт. Здесь является ссылкой для того же: