在開發(fā)中,經(jīng)常需要不同的字體徙邻,讓我們的app顯得有個性化點,那怎么樣去自定義字體呢?
字體查詢地址:https://fonts.google.com/
1.下載需要的字體ttf文件贬丛,導(dǎo)入工程
在Info.plist中添加一項:Fonts provided by application,填寫字體文件名稱加后綴.
1.png
2.前往target -> build phases -> Copy Bundle resources 把字體文件Add進(jìn)來
2.png
3. 打印出所以字體對應(yīng)的名字
- (void)fonts
{
NSArray *familyNames = [UIFont familyNames];
for( NSString *familyName in familyNames )
{
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for( NSString *fontName in fontNames )
{
printf( "\tFont: %s \n", [fontName UTF8String] );
}
}
}
4. 使用name自定義字體
- (void)buildLabel
{
self.firstLabel = [[UILabel alloc] init];
self.firstLabel.text = @"我哈尼哦搭訕ddd風(fēng)的地方";
self.firstLabel.font = [UIFont fontWithName:@"OpenSans" size:30];
[self.view addSubview:self.firstLabel];
self.firstLabel.frame = CGRectMake(10, 100, 380, 30);
self.secondLabel = [[UILabel alloc] init];
self.secondLabel.text = @"我哈尼哦搭訕ddd風(fēng)的地方";
[self.view addSubview:self.secondLabel];
self.secondLabel.font = [UIFont fontWithName:@"OpenSans-Italic" size:20];
self.secondLabel.frame = CGRectMake(10, 160, 260, 30);
}
2.png