我們平常項目開發用的字體基本都是系統默認的,但有時候設計為了追求完美,會使用自定義字體(當然得公司有錢買了版權哈),下面給大家講講怎么集成添加第三方字體。
1、導入三方字體文件進工程
我們就行平常添加文件一樣,將字體文件導入xcode工程內,一般字體文件是ttc/ttf/otf
如果測試需要可以去下載方正字體練練手https://ziti8.cc/list/12.htm
2、在info.plist文件告訴系統你所添加的字體
對應的添加Fonts provided by application可以,value是數組把你的自定義字體文件名寫入即可
3、先遍歷工程系統字體,找出你的自定義字體名字
for (NSString *fontfamilyname in [UIFont familyNames]) { NSLog(@"familyName:'%@'",fontfamilyname); for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) { NSLog(@" fontName:'%@'",fontName); } NSLog(@"***********"); }
檢索log,查出你的字體名稱
4、在文本顯示設置你的字體
為了有對比,我把默認系統字體也展示了
UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(30, 100, 240, 100); label.text = @"12345Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking"; label.font = [UIFont fontWithName:@"RevolutionGothic-Bold" size:13]; label.numberOfLines = 0; [self.view addSubview:label]; label = [[UILabel alloc] init]; label.frame = CGRectMake(30, 220, 240, 100); label.text = @"12345Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking"; label.font = [UIFont fontWithName:@"UTM-HelvetIns" size:13]; label.numberOfLines = 0; [self.view addSubview:label]; label = [[UILabel alloc] init]; label.frame = CGRectMake(30, 340, 240, 100); label.text = @"Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking"; label.numberOfLines = 0; label.font = [UIFont systemFontOfSize:13]; [self.view addSubview:label];
展示效果如圖
導入自定義字體功能就實現了,可以和你美術產品交差了啦。