0Pricing
SwiftUI Academy · レッスン

ButtonStyleとLabelStyle

ブランドに合わせて再利用できるコントロールスタイルを作ります。

「ButtonStyleとLabelStyle」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Style Controls, Not Instances

Restyling every button by hand gets tedious. A ButtonStyle defines one branded look you reuse across the whole app. 🎨

The ButtonStyle Protocol

Conform a struct to ButtonStyle and implement makeBody. It receives a configuration and returns the styled button.

struct PrimaryStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
    }
}

The label Inside Configuration

The configuration gives you the buttons label, which is the title or content. You decorate that label with padding and color.

configuration.label
    .padding()
    .background(.blue)

React to isPressed

Configuration also exposes isPressed. Use it to dim or scale the button while a finger is down for tactile feedback.

configuration.label
    .opacity(configuration.isPressed ? 0.6 : 1)

Apply with buttonStyle

Attach your style with the .buttonStyle modifier. Every button it touches adopts the same branded appearance.

Button("Save") { }
    .buttonStyle(PrimaryStyle())

Style a Whole Container

Set .buttonStyle on a stack and every button inside inherits it. One line styles a full group of controls.

Built-in Styles Exist Too

SwiftUI ships ready styles like .bordered and .borderedProminent. Reach for those before writing a custom one.

Button("Go") { }
    .buttonStyle(.borderedProminent)

Now Meet LabelStyle

A Label pairs text with an icon. A LabelStyle controls how that pair is arranged and shown.

Label("Home", systemImage: "house")

Title-Only or Icon-Only

Built-in label styles let you show just the title, just the icon, or both. Switch based on space without changing the call site.

Label("Home", systemImage: "house")
    .labelStyle(.iconOnly)

Custom LabelStyle

Conform to LabelStyle and implement makeBody to rearrange the icon and title yourself, like stacking them vertically.

func makeBody(configuration: Configuration) -> some View {
    VStack { configuration.icon; configuration.title }
}

One Definition, Many Buttons

Custom styles keep look and behavior in one place. Tweak the style and every button or label across the app updates together.

Quick Check

Quick check on control styles.

Recap: Reusable Control Looks

Use ButtonStyle and LabelStyle to define branded controls once. React to isPressed, rearrange labels, and apply the look everywhere. 🧠

よくある質問

「ButtonStyleとLabelStyle」レッスンは無料ですか?

はい。「ButtonStyleとLabelStyle」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「ButtonStyleとLabelStyle」で何を学びますか?

ブランドに合わせて再利用できるコントロールスタイルを作ります。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「ButtonStyleとLabelStyle」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 再利用可能なビューを切り出す
  2. カスタムViewModifier
  3. ButtonStyleとLabelStyle
  4. ViewBuilderとジェネリックコンテナ
← SwiftUI Academyに戻る