ios - Unicode在iOS中无法正常工作

标签 ios iphone unicode

将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>>

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:@"\\"];

最佳答案

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];

关于ios - Unicode在iOS中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32033313/

相关文章:

ios - 如何在数据库中的导航栏上显示标题

ios Autolayout - 在图像上对齐文本

iphone - 我该如何处理 Flurries 的累积?

iphone - 在 UIWebView 中显示来自相机的图像

sql - 无法在 SQL Azure for Mobile Services 上存储 Unicode 字符

php - 从 utf8_unicode_ci Mysql 表打印文本时出现错误字符

ios - UIWebView 上的双击识别 - Swift 3

ios - CJSONDeserializer IOS 错误

iphone - Obj-C,我可以通过 IAP 对我的免费版和完整版应用程序使用不同的目标吗?

Python BeautifulSoup 在写入文件时创建奇怪的\xe2 unicode 字符