在iOS開發中設置字體的方法有不少種,下面爲你們介紹比較經常使用的三種方法html
系統默認提供的字體主要是指UIFont中提供的字體,其使用代碼爲:ios
或者是經過字體詳細字典對字體屬性進行設置windows
/* UIFontDescriptorFamilyAttribute:設置字體家族名 UIFontDescriptorNameAttribute :設置字體的字體名 UIFontDescriptorSizeAttribute :設置字體尺寸 UIFontDescriptorMatrixAttribute:設置字體形變 */ UIFontDescriptor *attributeFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes: @{UIFontDescriptorFamilyAttribute: @"Marion", UIFontDescriptorNameAttribute:@"Marion-Regular", UIFontDescriptorSizeAttribute: @40.0, UIFontDescriptorMatrixAttribute:[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_1_PI*1.5) ]}]; fnotLabel.font = [UIFont fontWithDescriptor:attributeFontDescriptor size:0.0];
其中的字體家族名和字體名能夠經過如下方法獲取app
以上兩種方法都可覺得label設置字體,可是所有是隻針對英文數字,對中文無效。要想改變中文字體還須要使用後面兩種辦法async
iOS6之後蘋果就開始支持動態下載中文字體已供應用中展現個性字體的需求,因爲下載的時候須要使用的名字是PostScript名稱,須要使用Mac內自帶的應用「字體冊「來得到相應字體的PostScript名稱。以下顯示了從」字體冊「中獲取《娃娃體-繁 常規體》字體的PostScript名稱的截圖測試
具體代碼就不一一介紹了,你們能夠參考蘋果提供的有關文檔:https://developer.apple.com/library/ios/samplecode/DownloadFont/Listings/DownloadFont_ViewController_m.html#//apple_ref/doc/uid/DTS40013404-DownloadFont_ViewController_m-DontLinkElementID_6字體
或者也能夠參考唐巧先生的博客有比較詳細的介紹:http://blog.devtang.com/blog/2013/08/11/ios-asian-font-download-introduction/ui
下面是我研究後的測試demo,提供給你們參考:spa
- (void)asynchronouslySetFontName:(NSString *)fontName { UIFont* aFont = [UIFont fontWithName:fontName size:24]; // If the font is already downloaded if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) { // Go ahead and display the sample text. _fLabelView.text = @"歡迎查看個人博客"; _fLabelView.font = [UIFont fontWithName:fontName size:24]; return; } // Create a dictionary with the font's PostScript name. NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil]; // Create a new font descriptor reference from the attributes dictionary. CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs); NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0]; [descs addObject:(__bridge id)desc]; CFRelease(desc); __block BOOL errorDuringDownload = NO; // Start processing the font descriptor.. // This function returns immediately, but can potentially take long time to process. // The progress is notified via the callback block of CTFontDescriptorProgressHandler type. // See CTFontDescriptor.h for the list of progress states and keys for progressParameter dictionary. CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL, ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) { //NSLog( @"state %d - %@", state, progressParameter); double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue]; if (state == kCTFontDescriptorMatchingDidBegin) { dispatch_async( dispatch_get_main_queue(), ^ { // Show an activity indicator NSLog(@"Begin Matching"); }); } else if (state == kCTFontDescriptorMatchingDidFinish) { dispatch_async( dispatch_get_main_queue(), ^ { // Remove the activity indicator // Display the sample text for the newly downloaded font _fLabelView.text = @"歡迎查看個人博客"; _fLabelView.font = [UIFont fontWithName:fontName size:24]; // Log the font URL in the console CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)fontName, 0., NULL); CFStringRef fontURL = CTFontCopyAttribute(fontRef, kCTFontURLAttribute); NSLog(@"%@", (__bridge NSURL*)(fontURL)); CFRelease(fontURL); CFRelease(fontRef); if (!errorDuringDownload) { NSLog(@"%@ downloaded", fontName); } }); } else if (state == kCTFontDescriptorMatchingWillBeginDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { // Show a progress bar NSLog(@"Begin Downloading"); }); } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { // Remove the progress bar NSLog(@"Finish downloading"); }); } else if (state == kCTFontDescriptorMatchingDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { // Use the progress bar to indicate the progress of the downloading NSLog(@"Downloading %.0f%% complete", progressValue); }); } else if (state == kCTFontDescriptorMatchingDidFailWithError) { // An error has occurred. // Get the error message NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError]; if (error != nil) { _errorMessage = [error description]; } else { _errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!"; } // Set our flag errorDuringDownload = YES; dispatch_async( dispatch_get_main_queue(), ^ { NSLog(@"Download error: %@", _errorMessage); }); } return (bool)YES; }); }
只要在相應地方調用就能夠了:3d
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _fLabelView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 250, 100)]; [self.view addSubview:_fLabelView]; [self asynchronouslySetFontName:@"HanziPenSC-W3"]; }
下面是運行後的結果:
如今網上無論是windows字體,仍是Android字體只要是ttf格式的,或者是蘋果提供的ttc、otf格式,通常iOS程序都支持內嵌。具體作法:
先將須要下載的字體拖到項目中
在info文件中添加相應字段
而後就可使用上面提供的方法[UIFont fontWithName:@"迷你簡咪咪" size:17]方法給英文、數字或者中文設置上這種字體。能夠輸出一下[UIFont familyNames]檢測是否已經添加
也能夠在xib中爲label設置這種字體了
網上下載的字體也不必定都是可使用,下面提供你們一些經常使用字體供你們下載:
連接: http://pan.baidu.com/s/1kTVX8qF 密碼: vdwa
要想獲取更加全面的字體還可使用蘋果本身提供的各類字體格式,仍是能夠經過Mac應用「字體側」獲取,例如:
用法跟下載的字體同樣