【问题标题】:Unicode not working properly in iOSUnicode 在 iOS 中无法正常工作
【发布时间】:2015-08-16 08:45:41
【问题描述】:

当我将其设置为 UILabel 时,我的 unicode 是 \u20AC,但我在标签上得到了 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:@"\\"];

【问题讨论】:

  • @hasan83 请查看我的更新代码。
  • @halfer 是的,很有用
  • 您的代码看起来不错。您可以添加currencySymbol 的声明以及如何为其分配值。或者你有货币的nsarray?
  • 试试 priceLbl.text = [NSString stringWithFormat:@"%@ \u20ac",fair,currencySymbol];作为测试
  • @hasan83 来自服务器端的货币符号。当我试图在我的末尾设置相同的 unicode 获取欧元符号但是当它来自服务器端时它的打印 unicode。

标签: ios iphone unicode


【解决方案1】:

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

【讨论】:

  • 否,请查看更新后的答案。当我试图在我的最后设置这个 unicode 然后得到我想要的输出并且工作完美但来自服务器的货币符号和货币符号相同但打印 unicode。
  • 我知道。当它来自服务器时,它以字符串 @"\\u20AC" 的形式出现
  • 不,我用@“\”符号替换了它。请查看我更新的问题。
  • 那行不通,你的字符串中没有 \\\\ = \\ 你有 \\ = \。我试过了,那样不行。但 NGUYEN MINH 解决方案奏效了。
  • 非常感谢您的最后一个解决方案正常工作。
【解决方案2】:

请尝试:

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

【讨论】:

  • 当我尝试设置您的代码时,应用程序崩溃并无法访问。
【解决方案3】:

您将 NSLog 的输出与真实数据混淆了。

\u20ac 是 NSLog 显示欧元符号的方式,因为欧元符号是 Unicode 字符 u20ac。

【讨论】:

  • 请看我附在图片上的图片。它还在标签上打印 unicode。所以请看附图。
猜你喜欢
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
相关资源
最近更新 更多