最近項(xiàng)目中有使用三方字體的仑最,給大家分享一下乳讥,下面順便給大家一個下載字體的網(wǎng)址考蕾。
1.導(dǎo)入字體到工程中
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;
那么問題來了掖肋,我們?nèi)绾沃雷约簩?dǎo)入的字體的名稱呢甩苛?
步驟一:在加入字體前后分別運(yùn)行下面代碼蹂楣,并保存到兩個文件中,放到工程中
//打印都有哪些字體 新建文件把打印的東西賦值進(jìn)去
for(NSString *familyName in [UIFont familyNames])
{
NSLog(@"familyName = %@", familyName);
for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName])
{
NSLog(@"\tfontName = %@", fontName);
}
}
步驟二: 運(yùn)行下面的代碼
//未加入字體時候 File1
NSString *filePath11111 = [[NSBundle mainBundle]pathForResource:@"File1" ofType:nil];
NSString *str11111 = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:filePath11111] encoding:NSUTF8StringEncoding error:nil];
//每行打印放到數(shù)組中
NSArray *arr11111 = [str11111 componentsSeparatedByString:@"\n"];
//存放名字的干凈的數(shù)組
__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];
//每行打印放到數(shù)組中
NSArray *arr22222 = [str22222 componentsSeparatedByString:@"\n"];
//存放名字的干凈的數(shù)組
__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/