2012-08-01 39 views
3

我想創建一個使用用戶自定義字體的應用程序,因此我想讓我的應用程序在運行時加載ttf文件。 我熟悉通過UIAppFonts鍵添加自定義字體,如Adding custom font on iphone中所述。據推測,如果提供用戶創建的自定義版本,創建可由我的應用程序換出的佔位符自定義字體將不會成功?將iPhone/iPad文本動態更改爲自定義字體

+1

您無法在運行時更改應用程序包:http://stackoverflow.com/questions/4764591/replacing-images-in-the-bundle-at-run-time。您可以嘗試保存到文檔目錄,但我不確定UIFont是否會識別info.plist和應用程序捆綁包中找不到的字體。 – 2012-08-01 17:02:38

+0

我的想法是在包中放置一個「佔位符」字體,然後將該應用程序指向文檔目錄中的「真實」字體。但我沒有看到任何機制來指導程序在文檔目錄中查找字體。 – user216661 2012-08-06 22:43:59

+0

不幸的是,這聽起來不像是可能的。 – 2012-08-07 13:34:01

回答

3

我使用的字體文件是這樣的:http://www.webpagepublicity.com/free-fonts/a/Almagro%20Regular.ttf

而且我做到了使用此代碼:

CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontFilepath UTF8String]); 

// Create the font with the data provider, then release the data provider. 
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider); 
CGDataProviderRelease(fontDataProvider); 

CFErrorRef error = nil; 
CTFontManagerRegisterGraphicsFont(customFont, &error); 

CGFontRelease(customFont); 

if (error != nil) 
{ 
    NSError* err = (__bridge NSError*)error; 
    NSLog(@"error code %d desc: %@",err.code, [err description]); 
} 

UIFont* f = [UIFont fontWithName:@"Almagro" size:14.0]; 

有一些疑難雜症的雖然。您需要知道字體名稱,因爲它可能與文件名略有不同。在我的例子中,文件名是「Almagro Regular.ttf」,但字體名稱只是「Almagro」。在另一個論壇上有人說,你需要小心他們的名字中有空格的字體,因爲像「Sans Serif」這樣的字體會被註冊爲UFSont的「SansSerif」fontWithName:size:

讓我知道它是否有效。

+0

工作就像一個魅力。謝謝! – user216661 2012-08-12 20:46:30

相關問題