Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 24, 2008, 02:32 PM   #1
orangeiris
macrumors newbie
 
Join Date: Sep 2008
Using Custom Fonts

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?
orangeiris is offline   0 Reply With Quote
Old Sep 24, 2008, 02:37 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Yes. There is no way to load them.
robbieduncan is offline   0 Reply With Quote
Old Sep 24, 2008, 06:49 PM   #3
BNicholson
macrumors newbie
 
Join Date: Apr 2008
Quote:
Originally Posted by robbieduncan View Post
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
BNicholson is offline   0 Reply With Quote
Old Sep 25, 2008, 04:32 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by BNicholson View Post
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
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...
robbieduncan is offline   0 Reply With Quote
Old Sep 25, 2008, 07:30 AM   #5
Luke Redpath
macrumors 6502a
 
Join Date: Nov 2007
Quote:
Originally Posted by robbieduncan View Post
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.
Luke Redpath is offline   0 Reply With Quote
Old Sep 30, 2008, 05:00 PM   #6
orangeiris
Thread Starter
macrumors newbie
 
Join Date: Sep 2008
Quote:
Originally Posted by BNicholson View Post
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. And, of course, make sure I'm using a font with an appropriate license.
orangeiris is offline   0 Reply With Quote
Old Oct 1, 2008, 03:58 PM   #7
BNicholson
macrumors newbie
 
Join Date: Apr 2008
Quote:
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
BNicholson is offline   0 Reply With Quote
Old Oct 2, 2008, 02:48 PM   #8
helba
macrumors newbie
 
Join Date: Oct 2008
Quote:
Originally Posted by BNicholson View Post
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
helba is offline   0 Reply With Quote
Old Oct 22, 2008, 12:57 PM   #9
Warder
macrumors newbie
 
Join Date: Oct 2008
Fixed most of my other problems, how do I center the text?

Last edited by Warder; Oct 22, 2008 at 04:49 PM.
Warder is offline   0 Reply With Quote
Old Nov 13, 2008, 03:08 PM   #10
khushsingh88
macrumors newbie
 
Join Date: Nov 2008
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!
khushsingh88 is offline   0 Reply With Quote
Old Feb 2, 2009, 02:38 PM   #11
micksabox
macrumors newbie
 
Join Date: Jan 2009
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.
micksabox is offline   0 Reply With Quote
Old Mar 30, 2009, 10:42 AM   #12
supertoto
macrumors newbie
 
Join Date: Jul 2008
Just wondering

Does anybody get this done?
__________________
Macbook Pro 2.2G CPU, 2GB RAM, 80GB HD; 16GB iPhone 3G Black.
supertoto is offline   0 Reply With Quote
Old May 18, 2010, 04:03 AM   #13
sciencekris
macrumors newbie
 
Join Date: May 2010
Location: Chennai
Send a message via Yahoo to sciencekris
Custom Font

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


Quote:
Originally Posted by BNicholson View Post
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
sciencekris is offline   0 Reply With Quote
Old May 18, 2010, 05:00 AM   #14
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by sciencekris View Post
HI Friend , can you share a sample App code for adding custom Font..
See all that code in the post you quoted? That's it. Spoon-fed to you in tiny digestible chunks. What more can you possibly want?
robbieduncan is offline   0 Reply With Quote
Old Oct 8, 2013, 12:51 AM   #15
TinaTwilight
macrumors newbie
 
Join Date: Oct 2013
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.
__________________
plagiarism finder
TinaTwilight is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iFontz - Download and Install Custom Fonts for Use in a Variety of Apps charstacky iPhone and iPod touch Apps 0 Apr 14, 2014 02:29 AM
AnyFont Allows iOS Users to Install Custom Fonts for Use in a Variety of Apps MacRumors iOS Blog Discussion 27 Apr 3, 2014 06:25 AM
iOS7 - custom fonts in IB dantastic iPhone/iPad Programming 4 Sep 25, 2013 12:46 PM
Serious problem with fonts in Snow Leopard - can't use most fonts... Soom Mac Basics and Help 5 Jul 3, 2013 07:41 PM
Deleted duplicate fonts and now some fonts have changed? jent Mac OS X 10.7 Lion 7 Jun 26, 2012 02:06 AM

Forum Jump

All times are GMT -5. The time now is 06:20 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC