为何从Nib加载的UITextField默认字体过小且不一致?
Great question—let’s break down why you’re seeing this mismatch in default font sizes across your three text field implementations:
Nib/Storyboard vs. Code-Based UITextField Default Font Configurations
When you drag a UITextField into a XIB or Storyboard, Interface Builder sets its default font to a fixedSystem 14.0point size. This is a long-standing default for UIKit controls in visual editors. In contrast, when you create a UITextField programmatically (either directly in UIKit or under the hood of a SwiftUI TextField), it defaults to usingUIFont.systemFont(ofSize: UIFont.systemFontSize)—which on iOS is 17pt, aligned with the system’s.bodytext style. That’s the immediate size difference you’re seeing.Dynamic Type Support Discrepancy
Code-created UITextFields (including SwiftUI’s TextField) default to supporting Dynamic Type—meaning their font size adjusts automatically when the user changes their system text size settings. However, UITextFields from Nibs have “Use Dynamic Type” disabled by default in Interface Builder. This means their 14pt font stays fixed, even if the system’s default text size increases or decreases. This lack of dynamic support makes the inconsistency even more noticeable across different system settings.SwiftUI’s Abstraction Layer
SwiftUI’s TextField doesn’t just wrap a raw UITextField—it applies SwiftUI’s default typography rules, which tie directly to the system’s dynamic text styles (like.body). This ensures it stays aligned with other SwiftUI components and the system’s design guidelines, which is why it matches the programmatically created UITextField instead of the Nib-based one.
If you want to fix the inconsistency, you can either:
- Enable “Use Dynamic Type” for your Nib-based UITextField and set its font to the
.bodytext style in Interface Builder, or - Explicitly set the font programmatically for the Nib text field using
textField.font = UIFont.preferredFont(forTextStyle: .body)
内容的提问来源于stack exchange,提问作者Felix Marianayagam

