将unit设置为\u20AC
时,我的unicode为UILabel
,但标签上却出现了unicode。有时它正在打印欧元,但大多数情况下是在标签上打印unicode。
我的代码在这里
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];
输出
货币\ u20AC
Printing description of priceLbl:
<UILabel: 0x7faade3095a0; frame = (185 10; 50 25); text = '\u20AC'; userInteractionEnabled = NO; tag = 1001; layer = <_UILabelLayer: 0x7faadbd91bb0>>
我想尽一切可能设置输出。例如
获取服务器响应
{
currency = "\\u20AC";
description = "You have been successfully logged in.";
}
and the currency symbol replacing "\\" with "\"
NSString *currency = [response[@"currency"] stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
最佳答案
NGUYEN MINH的答案为我工作如下:
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];