2017-09-05 7 views
1

我試圖從自定義字體中顯示SAP圖標(請參閱附件鏈接)。我已將自定義字體SAP-icons.ttf添加到項目中(它包含在軟件包中並正確加載,雙重檢查),但我無法加載正確的符號。無法從「SAP-icons.ttf」字體中加載特定圖標

我使用的代碼片段如下:

獲取字體的路徑。它存儲在特定的包中。使用CGDataProvider獲取參考。

NSString *fontPath = [[NSBundle my_currentBundle]  pathForResource:@"SAP-icons" ofType:@"ttf"]; 

CGDataProviderRef provider = CGDataProviderCreateWithFilename([fontPath UTF8String]); 
CGFontRef fontRef = CGFontCreateWithDataProvider(provider); 
CFErrorRef error = nil; 
if (! CTFontManagerRegisterGraphicsFont(fontRef, &error)) { 
    CFStringRef errorDescription = CFErrorCopyDescription(error); 
    NSLog(@"Failed to load font: %@", errorDescription); 
    CFRelease(errorDescription); 
} 

獲取字體名稱並創建UIFont對象。

NSString *fontName = (NSString *)CFBridgingRelease(CGFontCopyPostScriptName(fontRef)); 
UIFont *font = [UIFont fontWithName:fontName size:20]; 

CFRelease(fontRef); 
CFRelease(provider); 

對於所需的圖標,我發現在網站&#xe202引用的unicode與下面的鏈接: https://sapui5.hana.ondemand.com/iconExplorer.html#/?tab=grid&search=info&icon=message-information

如果&#xe202 Unicode值是不正確的該圖標,我應該使用哪個? 我不能在我的Mac中安裝事件預覽SAP-icons字體字符 - 全部都是question marks

char *iconUnicodeStr = "&#xe202"; 
NSString *infoIconString = [NSString stringWithCString: iconUnicode encoding:(NSUnicodeStringEncoding)]; 

應用特定的字體,設置文本。結果 - 不正確的符號。

[self.button.titleLabel setFont:font]; 
[self.button.titleLabel setTitle:infoIconString]; 

在此先感謝您的任何幫助和建議,我怎麼能這樣做!取而代之的

char *iconUnicodeStr = "U+xe202"; 
label.text = [NSString stringWithCString:iconUnicodeStr encoding:(NSUnicodeStringEncoding)]; 

+0

您是否在info.plist中添加了? –

+0

是的,字體名稱添加在.plist文件中並正確加載。 – Dmitry

+0

您已給出名稱「SAP-icons.ttf」,並且您正在編寫代碼中的「SAP_icons」。不知道,可能是名稱**( - 和_)**問題。 –

回答

1

使用

label.text = [NSString stringWithFormat:@"%C", (unichar)0xe202]; 

而且不要忘記設置字體爲標籤

label.font = myCustomFont; 
+0

雙重檢查,它包含在構建階段。 – Dmitry

+0

你能用FontBook應用程序打開字體文件嗎?只是爲了確保字體名稱是正確的。(Command +空格式字體書) – joan

+0

經過檢查,字體名稱正確。 – Dmitry

0

我相信問題出在字體名稱。

for (NSString *familyName in [UIFont familyNames]){ 
    NSLog(@"Family name: %@", familyName); 
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) { 
     NSLog(@"--Font name: %@", fontName); 
    } 
} 

在AppDelegate didFinish中運行上面的代碼,並確保您使用的是正確的字體名稱。

從上面的代碼中搜索NSLog中的SAP_icons。如果你沒有找到正確的名字。

在上面的日誌中搜索SAP,會給你正確的名字。

+0

謝謝您的建議,但問題是正確加載特殊字符。請看答案。 – Dmitry