Open11
備忘録
たまに使う環境変数一覧
@Environment (\.presentationMode) private var presentationMode
@Environment(\.openURL) private var openURL
以下の記事で事済む。
Label
Label(
title: { Text("Jude Bellingham") },
icon: {
Image(systemName: "22.circle.fill")
.foregroundStyle(.orange)
}
)
Button("Send", systemImage: "paperplane") {}
.buttonStyle(.borderedProminent)
overlay & background
Circleは高さを持たない点に注意する。
Text("Sample")
.overlay {
Circle()
.foregroundStyle(Color(uiColor: .systemGray6))
.scaledToFill()
}
Divider()
.padding(.vertical)
Text("Sample")
.background {
Circle()
.foregroundStyle(Color(uiColor: .systemGray6))
.scaledToFill()
}
よく使うボタンの装飾
paddingが二つあるのは少々気持ち悪いが、サイズに合わせ自動的に調整してくれる。RoundedRectangleをclipShapeの引数に持たせたいが、CGFloatかCGSizeを指定する必要があるので、Capsuleで妥協。colorInvertを用いることでダークモードにも対応できる。
Text("Sample")
.foregroundStyle(.primary)
.colorInvert()
.font(.title)
.fontWeight(.semibold)
.padding()
.padding(.horizontal)
.background(.blue)
.clipShape(Capsule())
Structure: StrideTo, StrideThrough
let strideTo = stride(from: 0, to: 10, by: 2)
strideTo.forEach { print($0, terminator: " ") } // 0 2 4 6 8
print("\n- - -")
let strideThrough = stride(from: 0, through: 10, by: 2)
strideThrough.forEach { print($0, terminator: " ")} // 0 2 4 6 8 10
TimelineView
TimelineView(.periodic(from: .now, by: 1)) { context in
Text(context.date.description)
.font(.title2)
}
Modeifier: redacted
Apple Watchのアプリ作成の時だったか、ロック画面ウィジェットの作成だったかで使った気がする。
Modifier: textcase
ListのSectionのheaderの英文字は自動的に全てが大文字となる。大文字も小文字も自由に使用したいときはこのモディファイアを用いる。ちなみにnilを入れてもOK。
ContentUnavailableView
struct ContentView: View {
var body: some View {
ContentUnavailableView("該当者なし", systemImage: "person.slash", description: Text("条件を変更し再度お試しください。"))
ContentUnavailableView.search
ContentUnavailableView.search(text: "apple")
ContentUnavailableView(label: {
Label("No Mail", systemImage: "tray.fill")
}, description: {
Text("New mails you receive will appear here.")
}, actions: {
Button(action: {}) {
Text("Refresh")
}
})
}
}
Modifier: textSelection
テキストのコピーを可能にする。部分選択は不可。
Text("Sample")
.textSelection(.enabled)