Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
204: Dynamic Type Support
I've seen plenty of developers spend weeks polishing a UI, only to realize they've completely ignored accessibility. One of the most common oversights is Dynamic Type. If a user has bumped up their system font size because they actually need to see the text to use your app, and your layout stays frozen at 16pt, you've essentially locked them out.
Why your fixed font sizes are hurting users
The instinct is often to pick a "perfect" font size—say, 17pt for body text—and call it a day. But in iOS, we shouldn't be picking sizes; we should be picking roles. Apple provides "Text Styles" (like .headline, .body, and .caption1) that automatically scale based on the user's system settings.
Let's build a simple ArticlePreviewView. We want a bold headline and a short summary. If we do this the "old way," we're just setting a constant font. But to support Dynamic Type, we use the preferred font for a specific style.
class ArticlePreviewView: UIView {
let headlineLabel = UILabel()
let summaryLabel = UILabel()
func setupView() {
// Instead of UIFont.systemFont(ofSize: 20), we use:
headlineLabel.font = UIFont.preferredFont(forTextStyle: .headline)
summaryLabel.font = UIFont.preferredFont(forTextStyle: .body)
headlineLabel.text = "The Future of Swift"
summaryLabel.text = "Exploring the latest concurrency features and how they change the way we write async code."
summaryLabel.numberOfLines = 0 // Essential so text can wrap as it grows
}
}
The missing link: Real-time updates
Now, here is where I almost tripped up the first time I implemented this. I ran the app, went into the iOS Settings app, changed the text size to "Extra Large," and switched back to my app. Nothing happened. The text was still small. I thought the code was broken.
What I forgot is that UIFont.preferredFont only sets the font size at the moment the label is created. It doesn't automatically listen for system changes while the app is running. To make the label update its size dynamically without requiring a restart, you have to explicitly tell the label to adjust itself.
// This is the line I missed. Without this, Dynamic Type is only "half" working.
headlineLabel.adjustsFontForContentSizeCategory = true
summaryLabel.adjustsFontForContentSizeCategory = true
Adding those two lines tells UIKit to observe the UIContentSizeCategory.didChangeNotification and update the font immediately. It's a tiny bit of code, but it's the difference between a professional app and one that feels broken to an accessibility user.
Letting the layout breathe
Once the text actually scales, you'll notice a new problem: your layout will likely break. If you used hard-coded heights (like heightAnchor.constraint(equalToConstant: 100)), your text will either clip or overlap as it grows.
The solution is to lean heavily on UIStackView. Stacks are naturally flexible. By putting our labels in a vertical stack with alignment = .fill and distribution = .fill, the container will simply grow taller as the font size increases. I always recommend avoiding fixed heights whenever text is involved; let the content dictate the size of the container.
let stackView = UIStackView(arrangedSubviews: [headlineLabel, summaryLabel])
stackView.axis = .vertical
stackView.spacing = 8
stackView.translatesAutoresizingMaskIntoConstraints = false
addSubview(stackView)
// Constrain to edges, but NEVER set a fixed height for the stackView itself.
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: topAnchor, constant: 16),
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -16)
])📋 Practical Task
Refactoring the User Profile Card for Dynamic Type
You have been handed a UserProfileView that uses fixed font sizes and a fixed-height container, which makes the text clip when a user increases their system font size. Your task is to refactor this view to be fully accessible.
Requirements:
- Replace
UIFont.systemFont(ofSize: 22)for the username with the.title2preferred text style. - Replace
UIFont.systemFont(ofSize: 14)for the bio with the.subheadlinepreferred text style. - Enable real-time font updating for both labels so the user doesn't have to restart the app.
- Remove the fixed height constraint on the main container view and replace it with a
UIStackViewor flexible constraints that allow the view to expand vertically as the text grows.
There are no comments for now.