Is it true that you can't use custom fonts in apps? By custom, I mean any TTF that's not already available on the system?
Yes. There is no way to load them.
This is incorrect I am currently loading custom .ttf's and using them to draw text onto the screen with several of my applications. Look into CGFontCreateWithDataProvider and how to draw text in a - (void)drawRectCGRect)rect method.
Hope this helps!
-Brent
Of course you cannot distribute the fonts with your application without an appropriate license...
This is incorrect I am currently loading custom .ttf's and using them to draw text onto the screen with several of my applications. Look into CGFontCreateWithDataProvider and how to draw text in a - (void)drawRectCGRect)rect method.
Hope this helps!
-Brent
Thanks Brent! I'll look into this.
// Get the path to our custom font and create a data provider.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"FontToLoad" ofType:@"ttf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
// Create the font with the data provider, then release the data provider.
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
// 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);
- (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, kCGTextFill);
CGContextSetFillColorWithColor(context, self.textColor.CGColor);
CGContextSetFontSize(context, self.font.pointSize);
// Create an array of Glyph's the size of text that will be drawn.
CGGlyph textToPrint[[self.text length]];
// 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] - 23;
}
Heya orangeiris,
Since the NDA is lifted I would like to give you a little more of my code so you don't have to go through all of the pains I did trying to do this task.
1. I am using this code in a UILabel class override, which isn't really necessary at all you could get the same functionality out of a UIView override with a few more member variables.
2. Here is how I load the font:
Code:// Get the path to our custom font and create a data provider. NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"FontToLoad" ofType:@"ttf"]; CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]); // Create the font with the data provider, then release the data provider. CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider); CGDataProviderRelease(fontDataProvider); // 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);
3. Now after that is all completed you have loaded your first custom font! But this is where the task gets a little bit overly complicated, we need to draw with the font. All of the following code is placed in amethod.Code:- (void)drawRect:(CGRect)rect
Code:// 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, kCGTextFill); CGContextSetFillColorWithColor(context, self.textColor.CGColor); CGContextSetFontSize(context, self.font.pointSize); // Create an array of Glyph's the size of text that will be drawn. CGGlyph textToPrint[[self.text length]]; // 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] - 23; }
*****Please take note that I am subtracting 23 from the character's numeric value to get the correct ascii value. This may not be needed for your font or it maybe a different number. For the most part 23 seems to be the magic number for me, I have tried loading many fonts and the majority of them are correct with 23 and only two out of about a total of 25 I tried needed a different number. I honestly have no idea why this is needed other then to draw the correct characters. If you find a different way to do this or a better way let me know I am interested in seeing if there is a more refined way to approach this.******
From this point all that is needed is to actually draw the text.
I would suggest using one of the functions that are for "Drawing Glyphs". If you can't find what these are, search for the CGContext Reference it has them in there.
I have left out ALL of the code I have for modifying the spacing when drawing text as I felt it isn't relevant for the time being. Once you get all this working let me know and I will be more then willing to help you out getting the spacing fixed if you have any issues. Best of luck!
-Brent
Heya orangeiris,
Since the NDA is lifted I would like to give you a little more of my code so you don't have to go through all of the pains I did trying to do this task.
1. I am using this code in a UILabel class override, which isn't really necessary at all you could get the same functionality out of a UIView override with a few more member variables.
2. Here is how I load the font:
Code:// Get the path to our custom font and create a data provider. NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"FontToLoad" ofType:@"ttf"]; CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]); // Create the font with the data provider, then release the data provider. CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider); CGDataProviderRelease(fontDataProvider); // 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);
3. Now after that is all completed you have loaded your first custom font! But this is where the task gets a little bit overly complicated, we need to draw with the font. All of the following code is placed in amethod.Code:- (void)drawRect:(CGRect)rect
Code:// 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, kCGTextFill); CGContextSetFillColorWithColor(context, self.textColor.CGColor); CGContextSetFontSize(context, self.font.pointSize); // Create an array of Glyph's the size of text that will be drawn. CGGlyph textToPrint[[self.text length]]; // 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] - 23; }
*****Please take note that I am subtracting 23 from the character's numeric value to get the correct ascii value. This may not be needed for your font or it maybe a different number. For the most part 23 seems to be the magic number for me, I have tried loading many fonts and the majority of them are correct with 23 and only two out of about a total of 25 I tried needed a different number. I honestly have no idea why this is needed other then to draw the correct characters. If you find a different way to do this or a better way let me know I am interested in seeing if there is a more refined way to approach this.******
From this point all that is needed is to actually draw the text.
I would suggest using one of the functions that are for "Drawing Glyphs". If you can't find what these are, search for the CGContext Reference it has them in there.
I have left out ALL of the code I have for modifying the spacing when drawing text as I felt it isn't relevant for the time being. Once you get all this working let me know and I will be more then willing to help you out getting the spacing fixed if you have any issues. Best of luck!
-Brent
HI Friend , can you share a sample App code for adding custom Font..