在 Xcode 上的 UILabel 中显示货币符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14539377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:38:20  来源:igfitidea点击:

Displaying currency signs in UILabel on Xcode

xcodeunicodeuilabelcurrencyurlencode

提问by Fabrizio Bartolomucci

I am trying to display the gliphs for a currencies by using either the html format or unicode one. By using the former I tried all sorts of operations including: stringWithUTF8String, decodeFromPercentEscapeString, CFURLCreateStringByReplacingPercentEscapesUsingEncoding, stringByReplacingPercentEscapesUsingEncodingbut none of them succeded in turning € into a euro sign. With Unicode the issue was slightly better as:

我正在尝试使用 html 格式或 unicode 格式显示货币的 gliphs。通过使用前者,我尝试了各种操作,包括:stringWithUTF8StringdecodeFromPercentEscapeStringCFURLCreateStringByReplacingPercentEscapesUsingEncodingstringByReplacingPercentEscapesUsingEncoding但没有一个成功地将 € 转换为欧元符号。使用 Unicode,问题稍微好一些,因为:

NSString *aStr = [NSString stringWithUTF8String:[@"\u20ac" UTF8String]];

actually prints a euro sign, but for I reason I do not understand, if I provide the string as the result of a method, the unicode code gets displayed instead.

实际上打印了一个欧元符号,但出于我的原因我不明白,如果我提供字符串作为方法的结果,则会显示 unicode 代码。

What is the standard way for displaying euro, dollar or pounds signs in a UILbel?

在 UILbel 中显示欧元、美元或英镑符号的标准方式是什么?

回答by hwaxxer

UILabel automatically resolves unicode strings, no need to decode:

UILabel 自动解析 unicode 字符串,无需解码:

label.text = @"\u0024"; // dollar
label.text = @"\u20ac"; // euro

Refer to fileformat.infofor the encoding name.

有关编码名称,请参阅fileformat.info

回答by Anoop Vaidya

What about directly copying and pasting those characters in your string as :

直接将字符串中的这些字符复制和粘贴为:

NSString *aStr = @"Euro-, Dollar-$, Pound-£";
label.text=aStr;

回答by Fabrizio Bartolomucci

I found a very simple solution:

我找到了一个非常简单的解决方案:

float fareValue=/*float value*/;
NSNumber* fareNumber=[NSNumber numberWithFloat:fareValue];
NSString* formatted=[NSNumberFormatter localizedStringFromNumber:fareNumber numberStyle:NSNumberFormatterCurrencyStyle];

Thanks everyone

谢谢大家

回答by Matt Butler

Swift 3

斯威夫特 3

"\u{00A3}"

The '@' is no longer needed

不再需要“@”