пользовательский шрифт не работает на устройстве iphone - PullRequest
2 голосов
/ 16 января 2012

Я пытаюсь загрузить внешний шрифт .ttf в один из моих проектов iOS. Шрифт отлично работает в эмуляторе, но не отображается на реальном устройстве.

Я использую компилятор LLVM GCC 4.2. В другом проекте, с компилятором Apple LLVM 3.0, работает тот же шрифт. Я не понимаю, как я могу это исправить? Какие шаги мне нужно выполнить с компилятором LLVM GCC 4.2?

Ответы [ 4 ]

8 голосов
/ 19 апреля 2012

Убедитесь, что он добавлен в «Targets» -> «Build Phases» -> «Copy Bundle Resources». У меня была похожая проблема, и при ручном добавлении его в этот список на устройстве начал отображаться шрифт.

3 голосов
/ 16 января 2012

Для приведенного ниже кода шрифта «Справка»

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

Также вы можете загрузить образец кода и учебное пособие здесь.

2 голосов
/ 16 января 2012

У вас есть ограниченный выбор шрифтов, поэтому вы должны выбрать один из доступных шрифтов ...

Этот ответ полезен для справки: Какие шрифты поддерживают приложения iPhone?

1 голос
/ 16 января 2012

Сначала загрузите шрифт, как показано ниже:

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

}

В некоторых случаях вы можете не использовать некоторые загруженные из Интернеташрифт. Здесь является ссылкой для того же:

...