ios自定义字库

1、将.ttf(.otf)文件加入工程

2、在Copy Bundle Resources中添加.tff(.otf)文件

3、在info.plist中添加键Fonts provided by application,并添加.tff(.otf)文件

4、运行以下代码,获取family name和font name(font name可用)

    NSArray *familyName = [UIFont familyNames];
    NSArray *fontName;
    NSInteger i, j;
    for (i = 0; i < familyName.count; i++) {
        fontName = [[NSArray alloc] initWithArray:[UIFont fontNamesForFamilyName:[familyName objectAtIndex:i]]];
        NSLog(@"Family name:%@", [familyName objectAtIndex:i]);

        for (j = 0;j < fontName.count; j++) {
            NSLog(@"Font name:%@", [fontName objectAtIndex:j]);
        }
    }

输出结果:

...
2015-04-28 21:35:17.454 自定义字库[788:65391] Font name:TimesNewRomanPS-ItalicMT
2015-04-28 21:35:17.455 自定义字库[788:65391] Font name:TimesNewRomanPS-BoldMT
2015-04-28 21:35:17.455 自定义字库[788:65391] Family name:Georgia
2015-04-28 21:35:17.455 自定义字库[788:65391] Font name:Georgia-BoldItalic
2015-04-28 21:35:17.455 自定义字库[788:65391] Font name:Georgia
2015-04-28 21:35:17.455 自定义字库[788:65391] Font name:Georgia-Italic
2015-04-28 21:35:17.455 自定义字库[788:65391] Font name:Georgia-Bold
2015-04-28 21:35:17.455 自定义字库[788:65391] Family name:Gabriola
2015-04-28 21:35:17.456 自定义字库[788:65391] Font name:Gabriola
2015-04-28 21:35:17.456 自定义字库[788:65391] Family name:Arial Rounded MT Bold
2015-04-28 21:35:17.456 自定义字库[788:65391] Font name:ArialRoundedMTBold
2015-04-28 21:35:17.456 自定义字库[788:65391] Family name:Kailasa
...

在输出结果中搜索添加进去的字库名,如Gabriola,得到font name:name:Gabriola
5、调用类方法:

+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize;

如:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    label.text = @"asdfafdasdf";
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:@"Gabriola" size:30];

    [self.view addSubview:label];

显示结果

字体库可以从C:Windows/Fonts下获得
http://pan.baidu.com/s/1kTKfU8B

你可能感兴趣的:(ios自定义字库)