宣伝
アプリ作っているので入れてみてください!!
よろしくお願いいたします!!
本題
普段フォントとして、
.font(.title)などと使われているものは省略記法であり、実際は
.font(Font.title)
となります。
今回は好みでInt型に置き換えて、UserDefaultに保存します。
保存するために、Intに変換する
func getIntValue(from font: Font) -> Int {
switch font {
case .largeTitle:
return 10
case .title:
return 9
case .title2:
return 8
case .title3:
return 7
case .headline:
return 6
case .subheadline:
return 5
case .body:
return 4
case .callout:
return 3
case .caption:
return 2
case .caption2:
return 1
default:
return 0
}
}
保存されたIntからFontに変換する
func getFontValueFromInt(int: Int) -> Font {
switch int {
case 10:
return .largeTitle
case 9:
return .title
case 8:
return .title2
case 7:
return .title3
case 6:
return .headline
case 5:
return .subheadline
case 4:
return .body
case 3:
return .callout
case 2:
return .caption
case 1:
return .caption2
default:
return .body
}
}
UserDefaultから取得
self.font_size = getFontValueFromInt(int :UserDefaults.standard.integer(forKey: "font_size"))
セレクターで選ぶ。
VStack {
Picker("Font Size", selection: $selectedFontSize) {
Text("Title").tag(4)
Text("Title 3").tag(3)
Text("Body").tag(2)
Text("Caption").tag(1)
}
.pickerStyle(SegmentedPickerStyle())
.padding()
}
作ったアプリ。
では。