ButtonStyle และ LabelStyle
สร้างสไตล์ตัวควบคุมที่มีแบรนด์และนำกลับมาใช้ใหม่ได้
ButtonStyle และ LabelStyle เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ButtonStyle และ LabelStyle”
สร้างสไตล์ตัวควบคุมที่มีแบรนด์และนำกลับมาใช้ใหม่ได้ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ButtonStyle และ LabelStyle” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แยกมุมมองที่นำกลับมาใช้ใหม่ได้
- ViewModifiers แบบกำหนดเอง
- ButtonStyle และ LabelStyle
- ViewBuilder และคอนเทนเนอร์แบบทั่วไป