iOS中常見3種方法來控制字體,下面根據我在網上學習總結的內容發布(已完美避過所有坑,iOS8.4)
一.系統默認的設置字體方法(只對英文和數字生效的方法)
1.系統默認提供的字體主要是指UIFont中提供的字體,其使用代碼為:
_label.font = [UIFont fontWithName:@"Marion" size:20.0f];
2.或者是通過字體詳細字典對字體屬性進行設置
/*
UIFontDescriptorFamilyAttribute:設置字體家族名
UIFontDescriptorNameAttribute :設置字體的字體名
UIFontDescriptorSizeAttribute :設置字體尺寸
UIFontDescriptorMatrixAttribute:設置字體形變
*/
//創建描述字典
NSDictionary *fontDict = @{UIFontDescriptorFamilyAttribute:@"Marion",UIFontDescriptorNameAttribute:@"Marion-Regular",UIFontDescriptorSizeAttribute:@20.0f,UIFontDescriptorMatrixAttribute:[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_1_PI*1.5)]};
//創建字體描述對象
UIFontDescriptor *attributeFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:fontDict];
//設置字體
_label.font = [UIFont fontWithDescriptor:attributeFontDescriptor size:0.0];
其中的字體家族名和字體名可以通過以下方法獲取
NSLog(@"%@",[UIFont familyNames]);
以上兩種方法均可以為label設置字體,但是全部是只針對英文數字,對中文無效。要想改變中文字體還需要使用后面兩種辦法
二.將字體包直接打入工程,內嵌字體(對中英數字有效)
現在網上不管是windows字體,還是Android字體只要是ttf格式的,或者是蘋果提供的ttc、otf格式,一般iOS程序都支持內嵌
1.認識字體冊
MAC -> Launchpad -> 其他 -> 字體冊
2.打開字體冊后可以看到有各種中英文字體,可點擊各種按鈕試看功能,當前我們需要設置中文,如下圖所示,注意紅線部分
3.特別要注意這個PostScript名稱,這個名稱是我們在動態下載字體以及程序里設置Label字體時所要填寫的字體真實名稱,而非一會要提取的字體文件名稱
4.右鍵字體,選擇當前字體文件,如果字體尚未下載則先添加字體。拖入Xcode工程中,注意勾選復制以及tagert
5.如果上面一步沒有勾選復制或者targets,則會出現即使添加了字體並設置也沒有任何效果的狀況,這時候需要去Xcode設置中 -> Build Phases -> Copy Bundle Resources中手動添加當前的字體文件
6.在Xcode工程中的Info.plist文件中添加鍵Fonts provided by application,設置字體文件名為值(是字體文件名,要帶上擴展名,不一定跟PostScript同名)
7.設置Label字體名為當前導入字體的PostScrip名稱,即可使用
_label.font = [UIFont fontWithName:@"YuppySC-Regular" size:20.0f];
三.動態修改當前應用內字體(iOS6之后出現API,對中英數字有效)
這部分引用唐巧先生關於字體設置的博文,另外注意的是這個方法目前不完美,每次重啟APP時會重新自動下載字體並加載,初步判斷是因為字體下載到系統目錄而非內嵌應用沙盒導致的
唐巧博客:
http://blog.devtang.com/2013/08/11/ios-asian-font-download-introduction/
蘋果官方文檔:
1.首先依然需要去字體冊里尋找PostScript名稱,這次不管是下載還是設置都要使用這個名字
2.代碼示例
- (void)viewDidLoad {
[super viewDidLoad];
_label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 300, 20)];
_label.text = @"我懂得";
[self.view addSubview:_label];
BOOL is = [self isFontDownloaded:@"HanziPenSC-W3"];
if (is)
{
_label.font = [UIFont fontWithName:@"HanziPenSC-W3" size:15.0f];
}
else
{
[self downloadFontWithAppleServers:@"HanziPenSC-W3"];
}
}
- (BOOL)isFontDownloaded:(NSString *)fontName
{
UIFont *aFont = [UIFont fontWithName:fontName size:15.0f];
if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame))
{
return YES;
}
else
{
return NO;
}
}
- (void)downloadFontWithAppleServers:(NSString *)fontName
{
//用字體的PostScript名字創建一個字典
//注意:這個方法必須導入#import <CoreText/CoreText.h>
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName,kCTFontNameAttribute, nil];
//創建一個字體描述對象CTFontDescriptorRef
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
//將字體描述對象放到一個可變數組中
NSMutableArray *descArray = [NSMutableArray array];
[descArray addObject:(__bridge id)desc];
//定義一個下載錯誤變量
__block BOOL errorDuringDownload = NO;
//開始進行字體下載
CTFontDescriptorMatchFontDescriptorsWithProgressHandler((__bridge CFArrayRef) descArray, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) {
//獲取一個下載進度值
double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
if (state == kCTFontDescriptorMatchingDidBegin)
{
NSLog(@"字體已經匹配成功");
}
else if (state == kCTFontDescriptorMatchingDidFinish)
{
if (!errorDuringDownload)
{
NSLog(@"字體%@已經下載完成",fontName);
}
//返回主線程刷新
dispatch_async(dispatch_get_main_queue(), ^{
_label.font = [UIFont fontWithName:fontName size:15.0f];
});
}
else if (state == kCTFontDescriptorMatchingWillBeginDownloading)
{
NSLog(@"字體開始下載");
}
else if (state == kCTFontDescriptorMatchingDownloading)
{
NSLog(@"下載進度%.0f%%",progressValue);
}
else if (state == kCTFontDescriptorMatchingDidFinishDownloading)
{
NSLog(@"字體下載完成");
}
else if (state == kCTFontDescriptorMatchingDidFailWithError)
{
NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
NSString *errorMessage = @"";
if (error != nil)
{
errorMessage = [error description];
}
else
{
errorMessage = @"沒有可用的錯誤信息";
}
//設置標志
errorDuringDownload = YES;
NSLog(@"下載字體時發生錯誤:%@",errorMessage);
}
return (BOOL)YES;
});
}