2015-07-20 97 views
0

我想在我的iOS應用程序中使用truetype(.ttf)字體,它不工作。我已將該字體添加到我的項目並正確添加目標。我在應用程序提供的字體下的info.plist中添加了字體。我使用的是NSAttributedString顯示文本,這是我的代碼自定義字體不工作在Xcode

UIFont *hrFont = [UIFont fontWithName:@"ocrb" size:18.0]; 
NSDictionary *hrDict = [NSDictionary dictionaryWithObject:hrFont forKey:NSFontAttributeName]; 
NSMutableAttributedString *hrAttrString = [[NSMutableAttributedString alloc] initWithString:hrString attributes: hrDict]; 

UIFont *barcodeFont = [UIFont fontWithName:@"IDAutomation DataBar 34" size:22]; 
NSDictionary *barcodeDict = [NSDictionary dictionaryWithObject:barcodeFont forKey:NSFontAttributeName]; 
NSMutableAttributedString *barcodeAttrString = [[NSMutableAttributedString alloc]initWithString: alphaOnly attributes:barcodeDict]; 

[hrAttrString appendAttributedString:barcodeAttrString]; 

條形碼字體顯示正常,但在OCR字體顯示爲系統字體代替。任何幫助將不勝感激。

回答

4

添加您的自定義字體到你的項目,即拖字體 文件(ocrb.TTF)到XCode項目。

編輯Info.plist:使用密鑰添加一個新條目「 應用程序」提供的字體。

對於每一個文件,添加文件名到這個陣列

enter image description here

開業字體書(在取景器您的字體雙擊)的字體 看真正的文件名是什麼,我看到這個:

enter image description here

現在,字體設置爲您的標籤

yourLabel.font = [UIFont fontWithName:@"ocrb" size:15]; 
+0

我不得不在代碼中編寫實際調用字體的內容是[UIFont fontWithName:@「OCRB」size:15];原因不明。但它最終完美地工作。謝謝 –

+0

不客氣.... –

0

您可以檢查該字體是否通過使用下面的代碼

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); 
} 

加載與否如果沒有裝載您的字體,然後下載新的字體&將其添加到您的項目中。下載後&加入我的項目作品。