Dan*_*iel 11 objective-c storekit nsnumberformatter ios nslocale
我正在使用StoreKit我的应用程序中的应用程序内购买商店.
我有一个自定义设计,这意味着价格的价值应该是白色和大,货币符号更小,更暗,并与价格值的顶部对齐.
通过使用NSLocalein SKproduct的priceLocale属性和属性中的价格值,我可以毫无问题地获得货币符号price.
我的问题是知道何时应该在价格之前加上货币符号以及何时将货币符号放在价格之后.
例子:
我可以轻松地使用它NSNumberFormatter来"开箱即用",但由于我的布局为价值和货币符号定义了不同的风格,我发现自己处于需要更多手动解决方法的位置.
有什么想法吗 ?
tot*_*ter 19
您拥有SKProduct实例中所需的一切.只需NSNumberFormatter结合使用就可以了.
NSNumberFormatter *priceFormatter = [[NSNumberFormatter alloc] init];
[priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
for (SKProduct *product in response.products) {
[priceFormatter setLocale:product.priceLocale];
NSLog(@"Price for %@ is: %@",product.localizedTitle,[priceFormatter stringFromNumber:product.price]);
}
Run Code Online (Sandbox Code Playgroud)
let priceFormatter = NumberFormatter()
priceFormatter.numberStyle = .currency
for product in response.products {
priceFormatter.locale = product.priceLocale
let localizedPrice = priceFormatter.string(from: product.price)
print("Price for \(product.localizedTitle) is: \(localizedPrice)")
}
Run Code Online (Sandbox Code Playgroud)
区域设置对象似乎不直接提供此信息,但数字格式化程序当然必须知道它.您不应该直接询问(新式)数字格式化程序format,尽管这可能有用,然后您可以¤在格式字符串中查找货币符号.
可能更好的是创建一个CFNumberFormatter,它明确允许您查看其格式,然后检查该字符串:
// NSLocale and CFLocale are toll-free bridged, so if you have an existing
// NSNumberFormatter, you can get its locale and use that instead.
CFLocaleRef usLocale = CFLocaleCreate(NULL, CFSTR("en_US"));
CFNumberFormatterRef usFormatter = CFNumberFormatterCreate(NULL, usLocale, kCFNumberFormatterCurrencyStyle);
CFLocaleRef frLocale = CFLocaleCreate(NULL, CFSTR("fr_FR"));
CFNumberFormatterRef frFormatter = CFNumberFormatterCreate(NULL, frLocale, kCFNumberFormatterCurrencyStyle);
NSString * usString = (__bridge NSString *)CFNumberFormatterGetFormat(usFormatter);
NSString * frString = (__bridge NSString *)CFNumberFormatterGetFormat(frFormatter);
NSUInteger loc = ([usString rangeOfString:@"¤"]).location;
NSLog(@"Currency marker at beginning for US? %@", (loc == 0) ? @"YES" : @"NO");
loc = ([frString rangeOfString:@"¤"]).location;
NSLog(@"Currency marker at end for FR? %@", (loc == [frString length] - 1) ? @"YES" : @"NO");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6133 次 |
| 最近记录: |