教你如何在iOS項目中設置各種字體

在iOS開發(fā)中設置字體的方法有很多種秸应,下面為大家介紹比較常用的三種方法

1.使用系統(tǒng)默認提供的字體

系統(tǒng)默認提供的字體主要是指UIFont中提供的字體匀钧,其使用代碼為:

fontLabel.font = [UIFont fontWithName:@"Marion"size:17];

或者是通過字體詳細字典對字體屬性進行設置

/*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];

其中的字體家族名和字體名可以通過以下方法獲取

NSLog(@"familyNames:%@",[UIFont familyNames]);

以上兩種方法均可以為label設置字體,但是全部是只針對英文數字突颊,對中文無效。要想改變中文字體還需要使用后面兩種辦法

2.動態(tài)下載字體

iOS6以后蘋果就開始支持動態(tài)下載中文字體已供應用中展示個性字體的需求潘悼,由于下載的時候需要使用的名字是PostScript名稱律秃,需要使用Mac內自帶的應用“字體冊“來獲得相應字體的PostScript名稱。如下顯示了從”字體冊“中獲取《娃娃體-繁常規(guī)體》字體的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/

下面是我研究后的測試demo友绝,提供給大家參考:

- (void)asynchronouslySetFontName:(NSString *)fontName

{

UIFont* aFont = [UIFont fontWithName:fontName size:24];//If the font is already downloadedif(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:(__bridgeid)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);doubleprogressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];if(state ==kCTFontDescriptorMatchingDidBegin) {

dispatch_async( dispatch_get_main_queue(),^{//Show an activity indicatorNSLog(@"Begin Matching");

});

}elseif(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 consoleCTFontRef 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);

}

});

}elseif(state ==kCTFontDescriptorMatchingWillBeginDownloading) {

dispatch_async( dispatch_get_main_queue(),^{//Show a progress barNSLog(@"Begin Downloading");

});

}elseif(state ==kCTFontDescriptorMatchingDidFinishDownloading) {

dispatch_async( dispatch_get_main_queue(),^{//Remove the progress barNSLog(@"Finish downloading");

});

}elseif(state ==kCTFontDescriptorMatchingDownloading) {

dispatch_async( dispatch_get_main_queue(),^{//Use the progress bar to indicate the progress of the downloadingNSLog(@"Downloading %.0f%% complete", progressValue);

});

}elseif(state ==kCTFontDescriptorMatchingDidFailWithError) {//An error has occurred.//Get the error messageNSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];if(error !=nil) {

_errorMessage=[error description];

}else{

_errorMessage=@"ERROR MESSAGE IS NOT AVAILABLE!";

}//Set our flagerrorDuringDownload =YES;

dispatch_async( dispatch_get_main_queue(),^{

NSLog(@"Download error: %@", _errorMessage);

});

}return(bool)YES;? ? });? }

只要在相應地方調用就可以了:

- (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"];

}

下面是運行后的結果:

3.引入外部字體

現在網上不管是windows字體,還是Android字體只要是ttf格式的肝劲,或者是蘋果提供的ttc迁客、otf格式,一般iOS程序都支持內嵌辞槐。具體做法:

先將需要下載的字體拖到項目中

在info文件中添加相應字段

然后就可以使用上面提供的方法[UIFont fontWithName:@"迷你簡咪咪" size:17]方法給英文掷漱、數字或者中文設置上這種字體¢剩可以輸出一下[UIFont familyNames]檢測是否已經添加

也可以在xib中為label設置這種字體了

網上下載的字體也不一定都是可以使用卜范,下面提供大家一些常用字體供大家下載:

鏈接: http://pan.baidu.com/s/1kTVX8qF 密碼: vdwa

要想獲取更加全面的字體還可以使用蘋果自己提供的各種字體格式,還是可以通過Mac應用“字體側”獲取,例如:

用法跟下載的字體一樣

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末鹿榜,一起剝皮案震驚了整個濱河市海雪,隨后出現的幾起案子,更是在濱河造成了極大的恐慌舱殿,老刑警劉巖奥裸,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異沪袭,居然都是意外死亡湾宙,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來侠鳄,“玉大人埠啃,你說我怎么就攤上這事∥岸瘢” “怎么了碴开?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長知押。 經常有香客問我叹螟,道長,這世上最難降的妖魔是什么台盯? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮畏线,結果婚禮上静盅,老公的妹妹穿的比我還像新娘。我一直安慰自己寝殴,他們只是感情好蒿叠,可當我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蚣常,像睡著了一般市咽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上抵蚊,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天施绎,我揣著相機與錄音,去河邊找鬼贞绳。 笑死谷醉,一個胖子當著我的面吹牛,可吹牛的內容都是我干的冈闭。 我是一名探鬼主播俱尼,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼萎攒!你這毒婦竟也來了遇八?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤耍休,失蹤者是張志新(化名)和其女友劉穎刃永,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體羹应,經...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡揽碘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片雳刺。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡劫灶,死狀恐怖,靈堂內的尸體忽然破棺而出掖桦,到底是詐尸還是另有隱情本昏,我是刑警寧澤,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布枪汪,位于F島的核電站涌穆,受9級特大地震影響,放射性物質發(fā)生泄漏雀久。R本人自食惡果不足惜宿稀,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望赖捌。 院中可真熱鬧祝沸,春花似錦、人聲如沸越庇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽卤唉。三九已至涩惑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間桑驱,已是汗流浹背竭恬。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留碰纬,地道東北人萍聊。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像悦析,于是被迫代替她去往敵國和親寿桨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內容