【问题标题】:custom font not working on iphone device自定义字体在 iPhone 设备上不起作用
【发布时间】:2012-02-11 05:57:16
【问题描述】:

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

我正在使用 LLVM GCC 4.2 编译器。在另一个项目中,使用 Apple LLVM 编译器 3.0,同样的字体可以工作。我不明白我该如何解决?使用 LLVM GCC 4.2 编译器需要遵循哪些步骤?

【问题讨论】:

    标签: iphone objective-c ios5 xcode4.2 objective-c++


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      你也可以下载示例代码和教程here.

      【讨论】:

      • 感谢 neon,我使用的方法与您提到的相同,现在我弄错了,链接对我有帮助 link。字体名称是不同的 int case wise..silly 错误:(
      【解决方案3】:

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

      这个答案作为参考很有用: What fonts do iPhone applications support?

      【讨论】:

        【解决方案4】:

        首先加载如下字体:

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

        在某些情况下,您可能无法使用某些 Internet 下载的字体。 Here 是相同的参考:

        【讨论】:

        • 感谢 UPT 的帮助。与 Neon 的解决方案相比,使用您提到的方式有什么优势吗?我是一个新手,真的不太了解它..
        • 该解决方案也很好,但如果 iPhone 没有字体:“Grinched Font”,并且您想从您从某人那里购买的文件中导入它。此解决方案将帮助您实现它。
        • ohhh...感谢 UPT 提供的丰富信息...这将在未来帮助我,我现在很高兴 ..我正在设备上成功运行我的构建 ....再次感谢大家:)
        猜你喜欢
        • 1970-01-01
        • 2015-11-29
        • 2015-08-10
        • 1970-01-01
        • 2015-04-01
        • 1970-01-01
        • 2014-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多