1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

株式会社D2C IDAdvent Calendar 2022

Day 4

[SwiftUI] Textの金額表示を簡単に書く

Last updated at Posted at 2022-12-03

¥10,000 のような表記
SwiftではFormatterだったり、String.localizedStringWithFormatを使う方法がありますが
SwiftUIのTextであればFormatStyleを使って簡単に表示することができます。

import SwiftUI

struct SwiftUIView: View {
    let price = 1_000_000_000
    var body: some View {
        Text(price, format: IntegerFormatStyle.Currency.currency(code: "JPY"))
            .foregroundColor(.red)
            .fontWeight(.bold)
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

スクリーンショット 2022-11-01 11.37.22.png

省略して記載する方法を教えていただいたので記事修正しました。こっちのほうが簡単ですね。感謝🙇‍♀️

Text(price, format: .currency(code: "JPY"))

環境

Xcode 14.0.1
iOS 15.5以上

1
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?