2015-08-16 181 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。所以请看附件图片。 –