Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

orangeiris

macrumors newbie
Original poster
Sep 11, 2008
11
0
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?
 

BNicholson

macrumors newbie
Apr 7, 2008
14
0
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)drawRect:(CGRect)rect method.

Hope this helps!

-Brent
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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)drawRect:(CGRect)rect method.

Hope this helps!

-Brent

Well you learn something every day. Last time this was asked no-one had a solution. Of course you cannot distribute the fonts with your application without an appropriate license...
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
Of course you cannot distribute the fonts with your application without an appropriate license...

This is an important point that I think needs re-iterating. If you're using a custom font that you've purchased you need to check the license under which you purchased it as it will almost certainly preclude you from distributing the font itself.
 

orangeiris

macrumors newbie
Original poster
Sep 11, 2008
11
0
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)drawRect:(CGRect)rect method.

Hope this helps!

-Brent

Thanks Brent! I'll look into this. And, of course, make sure I'm using a font with an appropriate license.
 

BNicholson

macrumors newbie
Apr 7, 2008
14
0
Thanks Brent! I'll look into this.

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 a
Code:
- (void)drawRect:(CGRect)rect
method.

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
 

helba

macrumors newbie
Oct 2, 2008
2
0
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 a
Code:
- (void)drawRect:(CGRect)rect
method.

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

errr i'm a noob, how would call it?
Like I have a function, and I call the function from a button.
How would I call this in the function that the button is using?
Like I want to say Hello in a custom font? thanks =D
 

khushsingh88

macrumors newbie
Nov 13, 2008
4
0
Total noob

Hi im a total noob and need help on using this to load a gurmukhi ttf i have. all i want to do is type text into UITextView on interface builder and let the coding display using the ttf. I really hope this is possible and someone could be nice enough to post some code. I know its a hell of a lot to ask for. but you'd be helping out a lot.

thanks!
 

micksabox

macrumors newbie
Jan 30, 2009
1
0
Question...

Hey man, just wondering to the guy who got custom fonts to work:

What kind of member variables would you add if you were to load the font in a UIView class override? Also, where could you find the official font name to call, as I have been loading the font but it seems to create a NIL CGFontRef because the font name that I pass apparently doesnt work. For example, my font name is called Zit Graffiti but the filename is ZITGRAFF.TFF. When I search for it in Spotlight it gives me zit_graffiti. Any of these could be the name I should use, but which one is it?

I've been literally trying to get this to work for DAYS and would really appreciate any help frmo anybody who has gotten custom fonts to work in their apps. In most cases I follow the instructions exactly and I cannot get the custom font to appear.
 

sciencekris

macrumors newbie
May 18, 2010
1
0
Chennai
Custom Font

HI Friend , can you share a sample App code for adding custom Font..


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 a
Code:
- (void)drawRect:(CGRect)rect
method.

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
 

TinaTwilight

macrumors newbie
Oct 7, 2013
2
0
it isn't possible to style your text with a custom font embedded via CSS, while preventing people from downloading it. You need to use images, Flash, or the HTML5 Canvas, all of which aren't very practical.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.