自定义字体在 iPhone 设备上不起作用

发布于 2024-12-27 03:16:44 字数 176 浏览 4 评论 0原文

我正在尝试将外部 .ttf 字体加载到我的 iOS 项目之一中。该字体在模拟器中工作正常,但无法在实际设备上显示。

我正在使用 LLVM GCC 4.2 编译器。在另一个项目中,使用 Apple LLVM 编译器 3.0,可以使用相同的字体。我不明白我该如何解决它?使用 LLVM GCC 4.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城花音 2025-01-03 03:16:44

确保将其添加到“目标”下 -> '构建阶段' -> “复制捆绑资源”。我遇到了类似的问题,通过手动将其添加到此列表中,字体开始显示在设备上。

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.

满身野味 2025-01-03 03:16:44

对于下面的自定义字体代码帮助

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

您也可以下载示例代码和教程 这里。

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.

等数载,海棠开 2025-01-03 03:16:44

关于字体的选择有限,因此您必须在可用字体之间进行选择...

这个答案可作为参考:
iPhone 应用程序支持哪些字体?

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?

轮廓§ 2025-01-03 03:16:44

首先按如下方式加载字体:

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

}

在某些情况下,您可能无法使用某些互联网下载的字体。 这里是相同的参考:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文