我想有一个应用程序包括一个自定义字体渲染文本,加载它,然后使用它与标准UIKit元素如UILabel。这可能吗?


当前回答

我是这样做的:

加载字体:

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

现在,在你的drawRect:中,像这样做:

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

基本上,你必须在文本中进行一些暴力循环,并摆弄神奇的数字来找到字体中的偏移量(这里,看到我使用29),但它是有效的。

另外,你必须确保字体是合法嵌入的。大多数人都不是,有律师专门处理这类事情,所以要小心。

其他回答

iOS 3.2及后续版本支持此功能。直接摘自iPhone OS 3.2新增功能文档:

自定义字体支持 想要使用自定义字体的应用程序现在可以将这些字体包含在他们的应用程序包中,并通过在他们的信息中包含UIAppFonts键向系统注册这些字体。plist文件。该键的值是一个字符串数组,用于标识应用程序bundle中的字体文件。当系统看到键时,它加载指定的字体并使它们对应用程序可用。

一旦字体设置在信息。plist,您可以使用您的自定义字体作为任何其他字体在IB或编程。

苹果开发者论坛上有一个正在进行的话题: https://devforums.apple.com/thread/37824(需要登录)

这里有一个优秀而简单的三步教程,教你如何做到这一点(断开的链接被删除)

将您的自定义字体文件添加到您的项目中,使用Xcode作为资源 为你的信息添加一个键。名为UIAppFonts的plist文件。 将该键设置为数组 对于你拥有的每种字体,输入字体文件的全名(包括扩展名)作为UIAppFonts数组的项 保存Info.plist 现在在你的应用程序中,你可以简单地调用[UIFont fontWithName:@"CustomFontName" size:12]来获得自定义字体来使用你的UILabels和UITextViews等…

另外:确保字体在复制包资源中。

也许作者忘记给字体起一个Mac喜欢的名字了?

在FontForge中打开字体,然后进入元素>字体信息 有一个“Mac”选项,你可以设置喜爱的名称。 在文件>导出字体下,您可以创建一个新的ttf

你也可以试试导出对话框中的“Apple”选项。

免责声明:我不是IPhone开发者!

从iOS 4.1开始,有了一种使用自定义字体的新方法。它允许你动态加载字体,从文件中包含的应用程序,下载的数据,或你有什么。它还可以让你在需要的时候加载字体,而旧的方法是在应用启动时加载它们,如果你有很多字体,这可能会花费很长时间。

新方法在ios-dynamic-font-loading中描述

使用CTFontManagerRegisterGraphicsFont函数,给它一个带有字体数据的缓冲区。然后它就可以用于UIFont和web视图,就像旧的方法一样。下面是该链接的示例代码:

NSData *inData = /* your font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
    CFStringRef errorDescription = CFErrorCopyDescription(error)
    NSLog(@"Failed to load font: %@", errorDescription);
    CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);

首先将.odt格式的字体添加到资源中,在本例中我们将使用DINEngschriftStd。Otf,然后使用这段代码将字体分配给标签

[theUILabel setFont:[UIFont fontWithName:@"DINEngschriftStd" size:21]];

要确保你的字体被加载到项目中,只需调用

NSLog(@"Available Font Families: %@", [UIFont familyNames]);

在.plist中,必须声明字体。只需添加一个'由应用程序提供的字体'记录,并添加一个带有字体名称的item 0字符串(DINEngschriftStd.otf)

我在iOS 3.1.2上尝试了这个页面上的各种建议,以下是我的结论:

简单地使用[UIFont fontWithName:size:]与资源目录中的字体将不起作用,即使使用FontForge设置了FOND名称。

[UIFont fontWithName:size:]将工作,如果字体首先使用GSFontAddFromFile加载。但是GSFontAddFromFile不是iOS 3.1.2的一部分,所以它必须像@rpetrich所描述的那样动态加载。