Working with Text, Images, Buttons, and Stacks
# CHAPTER 12
Working with Text, Images, Buttons, and Stacks
1. Introduction
A layout is merely a skeleton. To bring an application to life, you must populate that skeleton with rich media and interactive elements. A professional iOS app requires beautiful typography, crisp icons, high-resolution imagery, and buttons that respond to human touch. In this chapter, we will master Text, Images, Buttons, and Stacks. We will explore advanced text manipulation, import external assets, utilize Apple's massive built-in icon library (SF Symbols), and engineer interactive buttons.2. Learning Objectives
By the end of this chapter, you will be able to:-
Apply advanced typography modifiers to the
Textview.
- Import and render local image assets into your Xcode project.
-
Utilize Apple's SF Symbols library using
Image(systemName:).
-
Create interactive
Buttonviews with custom layouts.
- Combine all components into a cohesive Profile Card mini-project.
3. Advanced Text Styling
SwiftUI allows for highly granular control over typography. Instead of guessing font sizes, Apple provides "Semantic Fonts" that automatically adapt to the user's accessibility settings (e.g., if a visually impaired user increases the system font size).4. Working with Images
To display a custom image (like a photo of a dog):-
1.
Open the Navigator panel in Xcode and click the
Assets.xcassetsfolder.
-
2.
Drag and drop a
.jpgor.pngfile into the Assets folder (let's say we name it"dog_photo").
-
3.
Call it in code using
Image("dog_photo").
By default, an Image will render at its massive, original resolution, blowing right off the screen! You MUST explicitly tell SwiftUI to allow resizing.
5. SF Symbols (Apple's Icon Library)
You do not need to download PNG files for standard icons (like settings gears, hearts, or magnifying glasses). Apple includes a massive, free vector library called SF Symbols directly inside iOS! You access them usingImage(systemName: "icon_name"). Because they are vectors, you can color and resize them exactly like text!
*(Tip: Download the free "SF Symbols" app from Apple's developer website to browse the thousands of available icon names).*
6. Buttons (Adding Interactivity)
AButton in SwiftUI has two distinct parts:
- 1. The Action: What happens when the user clicks it (Swift logic).
- 2. The Label: What the button looks like (UI Views).
7. Mini Project: Profile Card UI
Let's combineVStack, HStack, Text, Image, and Button to build a professional user profile card.
8. Common Mistakes
-
Forgetting
.resizable(): If you import a 4K resolution image into Xcode and writeImage("my_photo").frame(width: 100), the image will ignore your frame and take up the entire screen. In SwiftUI, an image is absolutely rigid until you explicitly add.resizable()before any frame modifiers.
9. Best Practices
-
Use
Labelfor Icon+Text: If you want a button that has a small icon next to text, do not manually build anHStackwith anImageand aText. Use the built-inLabel("Title", systemImage: "icon")component. It is highly optimized for accessibility and alignment!
10. Exercises
-
1.
Find an image on your computer, drag it into the
Assets.xcassetsfolder, and render it in a view as a 200x200 circle.
-
2.
Create an SF Symbol icon of a star (
star.fill) and make it yellow.
11. Coding Challenges
Challenge: Recreate the "Like" button from Instagram. Create aButton. Inside the label, use an Image with the system name "heart". When the button is clicked, print the word "Liked!" to the console.
12. MCQ Quiz with Answers
What specific modifier must be applied to a custom Image in SwiftUI before it will respect a .frame(width: height:) modifier and shrink down to fit the screen?
What is "SF Symbols"?
13. Interview Questions
-
Q: Explain the structural syntax of a
Buttonin SwiftUI. How does SwiftUI cleanly separate the logic execution block from the visual rendering block?
-
Q: Contrast the rendering behaviors of
Image("custom_asset")versusImage(systemName: "icon"). Why can you use.font()to resize the latter?
-
Q: What is the specific UI purpose of the
Labelcomponent, and why is it preferred over manually stacking an Image and Text view?
14. FAQs
Q: Can I use SF Symbols in Android apps? A: No. SF Symbols are heavily copyrighted by Apple. They are exclusively licensed for use within applications running on Apple platforms (iOS, macOS, watchOS, tvOS).15. Summary
In Chapter 12, we populated our layout skeletons with rich media. We applied granular semantic styling to theText view, ensuring our typography respects user accessibility. We successfully imported external media into the Assets catalog and manipulated it using the Image view, learning the absolute necessity of the .resizable() modifier. We integrated Apple's vast SF Symbols library for crisp vector iconography, and finally, we engineered interactive Button views cleanly separating business logic from visual labels.