2013-06-18 72 views
1

我使用自定義字體來顯示圖標。每個圖標都是一個字符。使用unicode代碼顯示字符

我可以通過副本UIControls使用它們/粘貼文字是這樣的:

myLabel.text = @""; 

或者與Unicode十六進制代碼:

myLabel.text = [NSString stringWithUTF8String:"\uf000"]; 

或者

myLabel.text = [NSString stringWithFormat:@"%c", 0xf000] 

現在我試圖使字符串中存儲的字符的十六進制代碼更加動態化爲NSString :

NSString *glassIcon = [charactersList objectForKey:@"icon-glass"]; //glassIcon = f000 

但我無法實現在我的標籤中顯示此字符。

我試圖

myLabel.text = [NSString stringWithFormat:@"%s", [subString UTF8String]]; 

myLabel.text = [NSString stringWithUTF8String:[subString UTF8String]]; 

但沒有這項工作的。

我敢肯定我錯過了一些明顯的東西,但我找不到。

回答

0

試試這個:

/* Convert it to an integer. */ 
uint16_t code = (uint16_t)strtol(glassIcon.UTF8String, NULL, 16); 

/* Convert the charcode to a string */ 
myLabel.text = [NSString stringWithFormat:@"%c%c", (char)((code >> 8) & 0xff), (char)(code & 0xff)]; 

你可能需要把這個在你的文件的頂部,不知道:

#include <string.h> 
+0

感謝你的幫助,但我不能使它工作。 – Dustt

+0

糟糕,我更新了它。請再試一次! :) – elslooo