2

I am trying to load an external .ttf font into one of my iOS projects. The font works fine within an emulator, but fails to display on an actual device.

I am using the LLVM GCC 4.2 compiler. In another project, with the Apple LLVM compiler 3.0, the same font works. I do not understand how I can fix it? What are the steps I need to follow with the LLVM GCC 4.2 compiler?

chetan rane
  • 533
  • 2
  • 10
  • 25

4 Answers4

8

Make sure it's added under 'Targets' -> 'Build Phases' -> 'Copy Bundle Resources'. I had a similar problem and by manually adding it to this list, the font started showing up on the device.

adjwilli
  • 9,658
  • 4
  • 35
  • 29
3

For the Custom font below code help

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 240, 40)];
[label1 setFont: [UIFont fontWithName: @"Grinched" size:24]];
[label1 setText:@"Grinched Font"];
[[self view] addSubview:label1];

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 240, 40)];
[label2 setFont: [UIFont fontWithName: @"Energon" size:18]];
[label2 setText:@"Energon Font"];
[[self view] addSubview:label2];

Also u can download sample code and tutorial here.

Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
  • thanks neon, i am using the same method as u mention, and now i got my my mistake the link helps me [link](http://stackoverflow.com/questions/5441764/custom-fonts-on-ios-app-working-in-simulator-but-not-ipad). the font name was different int case wise..silly mistake :( – chetan rane Jan 16 '12 at 09:26
2

You have a limited choice regarding fonts, so you have to choose between the fonts available...

This answer is useful as a reference: What fonts do iPhone applications support?

Community
  • 1
  • 1
AlbertoRR
  • 21
  • 3
1

First load the font like below:

(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);

}

Then use them as below:

-(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]);

}

There are some cases where you may not able to use some of the internet downloaded font. Here is the reference for the same:

UPT
  • 1,490
  • 9
  • 25
  • thank you UPT for your help. Is there any advantage of using the way you mention over Neon's solution? i am a newbie and really dont know much about it.. – chetan rane Jan 16 '12 at 09:28
  • 1
    That solution is also good but if iPhone does not have the font: "Grinched Font" and you want to import it from a file that you purchased from someone. This solution will help you to achieve it. – UPT Jan 16 '12 at 09:32
  • ohhh...thank you UPT for your fruitful info...this ll help me in future , m happy now ..i am running my build successfully on device ....thanks to all again :) – chetan rane Jan 16 '12 at 13:18