13

StoreKitアプリケーションにアプリ内購入ストアを実装するために使用しています。

私はカスタム デザインを持っています。これは、価格の値が白く大きく、通貨記号が小さく、暗く、価格値の上部に配置されることを意味します。

NSLocaleinSKproductpriceLocaleプロパティと、プロパティ内の価格の値を使用して、問題なく通貨記号を取得できpriceます。

私の問題は、いつ通貨記号を価格の前に置き、いつ価格の後に置くべきかを知ることです。

例:

  • $5.99
  • 0.79€</li>

を使用して簡単にNSNumberFormatterこれを「すぐに」解決できますが、私のレイアウトでは値と通貨記号に異なるスタイルが定義されているため、より手動の回避策が必要な状況に陥っています。

何かご意見は ?

4

6 に答える 6

21

インスタンスに必要なものはすべて揃ってい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]);
}

スイフト 3+

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)")
}
于 2013-04-30T21:31:14.487 に答える
9

ロケール オブジェクトはこの情報を直接提供していないようですが、もちろん数値フォーマッタはそれを認識している必要があります。(新しいスタイルの) 数値フォーマッターに直接要求することは想定されていませんが、おそらくうまくいくでしょう。その後、フォーマット文字列で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");
于 2012-09-03T21:45:49.273 に答える
2

私はこのソリューションを使用します(Swift):

let currencyFormat = CFNumberFormatterGetFormat(CFNumberFormatterCreate(nil, locale, .CurrencyStyle)) as NSString
let positiveNumberFormat = currencyFormat.componentsSeparatedByString(";")[0] as NSString
let currencySymbolLocation = positiveNumberFormat.rangeOfString("¤").location
return (currencySymbolLocation == 0) ? .Before : .After

CFNumberFormatterGetFormat(一部のロケールでは) が double 値を返すことがあるため、受け入れられた回答を修正する必要があります。¤##,#00.0;-¤##,#00.0これには負の数値形式が含まれます。その文字列を必ず解析してください。

于 2016-04-26T17:20:15.343 に答える