2015-08-16 175 views
1

我的Unicode是\u20AC當我將它設置爲UILabel但我在標籤上得到unicode。有一段時間它打印歐元,但大部分時間是在標籤上打印unicode。Unicode在iOS上無法正常工作

我的代碼是在這裏

NSLog(@"Currecy %@",currencySymbol); 
    UILabel *priceLbl = [[UILabel alloc] initWithFrame:CGRectMake(180, 10, 45, 25)]; 

    priceLbl.textColor = [UIColor whiteColor]; 
    priceLbl.textAlignment = NSTextAlignmentCenter; 
    priceLbl.font = [UIFont fontWithName:@"ProximaNova-Bold" size:15]; 
    priceLbl.tag = 1001; 
    fair = [NSString stringWithFormat:@"%d",arc4random()%50]; 
    priceLbl.text = [NSString stringWithFormat:@"%@ %@",fair,currencySymbol]; 

輸出 Currecy \ u20AC

Printing description of priceLbl: 
<UILabel: 0x7faade3095a0; frame = (185 10; 50 25); text = '\u20AC'; userInteractionEnabled = NO; tag = 1001; layer = <_UILabelLayer: 0x7faadbd91bb0>> 

enter image description here

而且我想我最終獲得輸出,我會設置喜歡。例如

enter image description here

獲取服務器響應

{ 
    currency = "\\u20AC"; 
    description = "You have been successfully logged in."; 
} 

and the currency symbol replacing "\\" with "\" 

NSString *currency = [response[@"currency"] stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"]; 
+0

你的代碼在哪裏? – hasan83

+0

@ hasan83請參閱我更新的代碼。 –

+0

@halfer是的,它是有用的 –

回答

3

阮答案爲我工作如下:

NSString *currencySymbol = @"\\u20AC"; 
NSString *fair = @"1.99 "; 

NSString *convertedString = currencySymbol; 

CFStringRef transform = CFSTR("Any-Hex/Java"); 
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES); 

label.text = [NSString stringWithFormat:@"%@ %@", fair, convertedString]; 

的問題是,你的CURRENCYSYMBOL包含:@"\\u20AC"這是6個字符,而不是一個字符串@"\u20AC"

另一種可行的解決方案的字符串:

NSString *currencySymbol = @"\\u20AC"; 
NSString *fair = @"1.99 "; 

currencySymbol = [NSString 
        stringWithCString:[currencySymbol cStringUsingEncoding:NSUTF8StringEncoding] 
        encoding:NSNonLossyASCIIStringEncoding]; 


label.text = [NSString stringWithFormat:@"%@ %@", fair, currencySymbol]; 
+0

不,請參閱最新的答案。當我試圖在我的最後設置這個unicode然後得到輸出,因爲我想和工作完美,但貨幣符號來自服務器和貨幣符號是相同的,但打印unicode。 –

+0

我知道。當它來自服務器它來作爲一個字符串@「\\ u20AC」 – hasan83

+0

不,我用@「\」符號替換它。並請看我最新的問題。 –

1

請嘗試:

NSString *convertedString = @"some text"; //Your unicode string here 
CFStringRef transform = CFSTR("Any-Hex/Java"); 
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES); 
yourLabel.text = convertedString; 
+0

當我試圖設置你的代碼時,應用程序崩潰並給予錯誤的訪問權限。 –

0

你感到困惑的NSLog與實際數據的輸出。

\ u20ac是NSLog如何顯示歐元符號,因爲歐元符號是Unicode字符u20ac。

+0

請看我附上的圖片,我會在圖像上展示你。它還在標籤上打印unicode。所以請看附件圖片。 –