Loading Fonts

有兩種在應(yīng)用中加載字體的方式。第一種方法是更改應(yīng)用的plist文件, 僅適用于程序編寫者欲鹏;另一種load fonts dynamically,也適用于框架編寫者膘盖。

Load fonts within an app

參考來源:http://stackoverflow.com/questions/21737788/custom-fonts-in-ios-7
First of all I'm assuming that SpriteKit doesn't make any difference.

  1. You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
  2. You need to edit .plist file. Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
  3. Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.

Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:

NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++) {
  NSString *fontFamily = [fontFamilies objectAtIndex:i];
  NSArray *fontNames = [UIFont fontNamesForFamilyName:   [fontFamilies objectAtIndex:i]];
  NSLog (@"%@: %@", fontFamily, fontNames);
}

Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light". After that you only need to use that font by:

UIFont *customFont = [UIFont fontWithName:@"JosefinSansStd-Light" size:20];

In iOS8 you add your fonts directly to the project and they are visible in the interface builder. Modify your code to account for this but programmatically setting font for iOS7 and selecting it in xCode6 interface builder. PS. Interface builder in xCode6 gives you the correct font name that you can copy-paste into the code below.

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

if(SYSTEM_VERSION_LESS_THAN(@"8.0")) {
  UIFont *customFont = [UIFont fontWithName:@"OpenSans-Light" size:32];
  self.registerLabel.font = customFont;  
}

Load fonts dynamically

參考來源:http://www.marco.org/2012/12/21/ios-dynamic-font-loading

- (void)loadFont {
  NSData *inData = /* your decrypted font-file data */;
  CFErrorRef error;

  CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
  CGFontRef font = CGFontCreateWithDataProvider(provider);

  if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
  CFStringRef errorDescription = CFErrorCopyDescription(error)
  NSLog(@"Failed to load font: %@", errorDescription);
  CFRelease(errorDescription);
  }

  CFRelease(font);
  CFRelease(provider);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末损晤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子尤勋,更是在濱河造成了極大的恐慌,老刑警劉巖揖盘,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锌奴,死亡現(xiàn)場離奇詭異憾股,居然都是意外死亡,警方通過查閱死者的電腦和手機茴恰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門往枣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來粉渠,“玉大人,你說我怎么就攤上這事霸株。” “怎么了坡椒?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長汗唱。 經(jīng)常有香客問我丈攒,道長,這世上最難降的妖魔是什么识椰? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任深碱,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘力奋。我一直安慰自己幽七,他們只是感情好,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布猿挚。 她就那樣靜靜地躺著驶鹉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪办绝。 梳的紋絲不亂的頭發(fā)上姚淆,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機與錄音昔驱,去河邊找鬼上忍。 笑死纳本,一個胖子當著我的面吹牛腋颠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播巾腕,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼絮蒿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了佛寿?” 一聲冷哼從身側(cè)響起但壮,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎弹渔,沒想到半個月后溯祸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡鸟召,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了压状。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡镣丑,死狀恐怖娱两,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情趣竣,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布卫袒,位于F島的核電站单匣,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏户秤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一泡徙、第九天 我趴在偏房一處隱蔽的房頂上張望膜蠢。 院中可真熱鬧,春花似錦礁竞、人聲如沸杉辙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽品腹。三九已至,卻和暖如春舞吭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蔑穴。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工惧浴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人捐腿。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像倡蝙,于是被迫代替她去往敵國和親绞佩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容