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


当前回答

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

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

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

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

其他回答

目前还没有发布,但下一个版本的cocos2d (2d游戏框架)将支持可变长度的位图字体作为角色地图。

http://code.google.com/p/cocos2d-iphone/issues/detail?id=317

作者没有确定这个版本的发布日期,但我确实看到一个帖子说它将在未来一两个月发布。

作为一个增强@bdev的答案,这里是一个更新版本列出自定义字体。

步骤1:使用@bdev的答案找出所有系统字体并保存到文件中。

将以下代码放在第一个视图控制器的-(void)viewDidLoad中,在[super viewDidLoad]之后(或在App Delegate中):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSMutableArray *system_fonts = [NSMutableArray array];
for (NSString *familyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
        [system_fonts addObject:fontName];
    }
}
if([paths count] > 0) {
    [system_fonts writeToFile:[[paths objectAtIndex:0]
                               stringByAppendingPathComponent:@"array.out"] atomically:YES];
}

运行应用程序一次。事后停止。

步骤2:添加自定义字体到项目

使用已接受的答案中显示的方法,添加您的自定义字体(记得更新.plist并通过勾选add to Target添加要构建的字体文件。

步骤3:将系统字体与当前字体列表进行比较

将步骤1中的代码替换为:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSMutableArray *system_fonts = [NSMutableArray arrayWithContentsOfFile:[[paths objectAtIndex:0]
                                                                        stringByAppendingPathComponent:@"array.out"]];

for (NSString *familyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
        if (![system_fonts containsObject:fontName]) {
            NSLog(@"%@", fontName);
        }
    }
}

运行App,你添加的自定义字体列表就会显示出来。

这适用于iOS 3.2到iOS 6(未来的版本可能会正常工作)。也适用于.ttc和.ttf。

这是如何做这件事的一步一步的说明。不需要额外的库或任何特殊的编码。

http://shang-liang.com/blog/custom-fonts-in-ios4/

大多数时候问题在于字体而不是方法。最好的方法是使用一种肯定有效的字体,比如verdana或geogia。然后更改为想要的字体。如果它不起作用,可能是字体名称不正确,或者字体格式不正确。

我建议大家参考我最喜欢的一个简短教程:http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/,上面的信息都来自这个网站。

第1步-将.ttf或.otf从Finder拖到项目中

注意-请确保在主应用程序目标上单击“添加到目标”框

如果你忘记单击将其添加到目标,然后单击项目层次结构中的字体文件,在右侧面板中单击目标会员部分中的主应用程序目标

要确保字体是应用目标的一部分,请确保它们出现在构建阶段的Copy Bundle Resources中

步骤2 -添加字体文件名到你的Plist

去自定义iOS目标属性在你的信息部分,并添加一个关键字,在该部分的项目,称为字体提供的应用程序(你应该看到它作为一个选项,当你输入它,它将自己设置为一个数组。点击小箭头打开数组项,输入你添加的.ttf或.otf文件的名称,让你的应用程序知道这些是你想要可用的字体文件

注意-如果你的应用程序在这一步之后崩溃,那么检查你在这里添加的项目的拼写

步骤3 -找出字体的名称,这样你就可以调用它们

通常情况下,应用程序看到的字体名称与基于该字体的文件名所认为的不同,将其放入代码中,并查看应用程序所做的日志,以查看在代码中调用的字体名称

斯威夫特

for family: String in UIFont.familyNames(){
  print("\(family)")
  for names: String in UIFont.fontNamesForFamilyName(family){
      print("== \(names)")
  }
}

Objective - C

for (NSString* family in [UIFont familyNames]){
    NSLog(@"%@", family);
    for (NSString* name in [UIFont fontNamesForFamilyName: family]){
        NSLog(@"  %@", name);
    }
}

你的日志应该是这样的:

步骤4 -使用步骤3中的名称使用新的自定义字体

斯威夫特

 label.font = UIFont(name: "SourceSansPro-Regular", size: 18)

Objective - C

 label.font = [UIFont fontWithName:@"SourceSansPro-Regular" size:18];

我是这样做的:

加载字体:

- (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),但它是有效的。

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