iOS中三方字体的使用

最近项目中有使用三方字体的,给大家分享一下,下面顺便给大家一个下载字体的网址。

1.导入字体到工程中

step1.png

2.在info.plist文件中添加

step2.png

3.使用字体

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.aaa.text];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Cloudtype-HJSweibeiGB" size:36] range:NSMakeRange(0, str.length)];
 self.showLable.attributedText = str;

那么问题来了,我们如何知道自己导入的字体的名称呢?

步骤一:在加入字体前后分别运行下面代码,并保存到两个文件中,放到工程中

//打印都有哪些字体  新建文件把打印的东西赋值进去
for(NSString *familyName in [UIFont familyNames])
{
    NSLog(@"familyName = %@", familyName);
    for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName])
    {
        NSLog(@"\tfontName = %@", fontName);
    }
}

步骤二: 运行下面的代码

//未加入字体时候 File1

NSString *filePath11111 = [[NSBundle mainBundle]pathForResource:@"File1" ofType:nil];
NSString *str11111 = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:filePath11111] encoding:NSUTF8StringEncoding error:nil];
//每行打印放到数组中
NSArray *arr11111 = [str11111 componentsSeparatedByString:@"\n"];
//存放名字的干净的数组
__block NSString *tempStr11111 ;
NSMutableArray *tempArr11111 = [NSMutableArray array];
[arr11111 enumerateObjectsUsingBlock:^(NSString * str , NSUInteger idx, BOOL * _Nonnull stop) {
    //获取等号的位置
    if (str.length > 0) {
        NSRange range = [str rangeOfString:@"="];
        tempStr11111 = [str substringFromIndex:(range.location + 2)];

        [tempArr11111 addObject:tempStr11111];

    }
}];





//加入字体后的  File2

NSString *filePath22222 = [[NSBundle mainBundle]pathForResource:@"File2" ofType:nil];
NSString *str22222 = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:filePath22222] encoding:NSUTF8StringEncoding error:nil];
//每行打印放到数组中
NSArray *arr22222 = [str22222 componentsSeparatedByString:@"\n"];
//存放名字的干净的数组
__block NSString *tempStr22222 ;
NSMutableArray *tempArr22222 = [NSMutableArray array];
[arr22222 enumerateObjectsUsingBlock:^(NSString * str , NSUInteger idx, BOOL * _Nonnull stop) {
    //获取等号的位置
    if (str.length > 0) {
        NSRange range = [str rangeOfString:@"="];
        tempStr22222 = [str substringFromIndex:(range.location + 2)];

        [tempArr22222 addObject:tempStr22222];

    }
}];



//判断那些字体是你引入的

for (int i = 0; i < tempArr11111.count; i++) {
    if (![tempArr22222 containsObject:tempArr11111[i]]) {
        NSLog(@"%@",tempArr11111[i]);
    }
}

下面打印出来的字体的名称
ziti.png

效果图:


效果图.png

附上字体下载的链接 http://www.zhaozi.cn/s/all/ttf/

你可能感兴趣的:(iOS中三方字体的使用)