Is there a way in SwiftUI to detect if a user has Larger Text size enabled?
There is an environment value for that:
@Environment(\.sizeCategory) var sizeCategory
With that, you can do stuff like:
if sizeCategory > ContentSizeCategory.large {
// views for large text
} else {
// views for regular/small text
}
You should also check out the @ScaledMetric
property wrapper, which will auto-scale your var based on the user’s text size:
@ScaledMetric var height: CGFloat = 100
Here's a nice summary of both: https://swiftwithmajid.com/2019/10/09/dynamic-type-in-swiftui/
iOS Swift how to detect Dynamic Type Sizes with customer Font
Watch for the trait collection to change and read the preferredContentSizeCategory
. If it has changed, mess with the interface as desired.
How to detect Dynamic Font size changes from iOS Settings?
You listen for the Size Change Notification on UIContentSizeCategory.
Swift 3.0:NSNotification.Name.UIContentSizeCategoryDidChange
Swift 4.0 or later:UIContentSizeCategory.didChangeNotification
Possible to detect Bold Text setting in Settings Accessibility?
As of iOS 8, it is possible to detect whether the user has enabled Bold Text in Settings using UIAccessibility.isBoldTextEnabled
(docs) and UIAccessibility.boldTextStatusDidChangeNotification
(docs).
For apps that also require iOS 7 support, I’ve written an elegant one-liner that works on iOS 7 & 8 with Helvetica Neue and even on iOS 9 with the San Francisco typeface, based on the fact that standard-weight fonts are commonly referred to as the “Regular” weight, and that body text uses this weight for readability:
Objective-C:
BOOL hasBoldText = ![[UIFont preferredFontForTextStyle:UIFontTextStyleBody].fontName hasSuffix:@"-Regular"];
Swift:
let hasBoldText = !UIFont.preferredFontForTextStyle(UIFontTextStyleBody).fontName.hasSuffix("-Regular")
Is there a faster way to change accessibility (dynamic type) text size on iOS simulator?
The best way to reach your goal depends on the Xcode version you're using.
In Xcode 10, go to the Xcode
- Open Developer Tool
menu and select the Accessibility Inspector
element:
- If you haven't run your simulator, do it now and select it in the panel.
- Select the
Settings
button to display the different text sizes. - Test your dynamic type implementation by changing the font size.
If you need further explanation about this tool, I strongly recommend to take a look at these WWDC detailed summaries that contain very useful information: 2016 and 2019.
In Xcode 11, there's an option directly accessible from the debug bar in the LOG window: if you haven't run your simulator, do it now and select the LOG window in Xcode:
- Select the
Environment Overrides
button to open the new pane. - Switch the TEXT on, vary the text size and dynamically visualize its rendering on the simulator.
Using these tools is, in my view, the fastest way to test the Dynamic Type feature implementation.
⚠️ EDIT 2022/06/24 ⚠️
SwiftUI in Xcode 14 provides a new live previews that are interactive by default: your changes are immediately updated in the canvas and displayed in all sizes as you make them. br>
Size a UILabel in SwiftUI via UIViewRepresentable like Text to wrap multiple lines
The problem here is in ScrollView
which requires definite height, but representable does not provide it. The possible solution is to dynamically calculate wrapped text height and specify it explicitly.
Note: as height is calculated dynamically it is available only in run-time, so cannot be tested with Preview.
Tested with Xcode 12 / iOS 14
struct LabelView: View {
var text: String
@State private var height: CGFloat = .zero
var body: some View {
InternalLabelView(text: text, dynamicHeight: $height)
.frame(minHeight: height)
}
struct InternalLabelView: UIViewRepresentable {
var text: String
@Binding var dynamicHeight: CGFloat
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return label
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.text = text
DispatchQueue.main.async {
dynamicHeight = uiView.sizeThatFits(CGSize(width: uiView.bounds.width, height: CGFloat.greatestFiniteMagnitude)).height
}
}
}
}
Newbie question. How to set TextField's horizontal size to N characters
Use a fixed size TextField
.
TextField("******", text: $text).fixedSize()
How to test Dynamic Type (larger font sizes) in iOS Simulator
As of Xcode 8, there is a better option than the undocumented launch arguments: Accessibility Inspector. It is covered in this talk and can be found under Xcode > Open Developer Tool.
Update for Xcode 11+
There is a new Environment Overrides option. Find more information in this answer.
Related Topics
How to Change Orientation for Avcapturemoviefileoutput in Swift
How to Distinguish Bool and Int in Swift
Swift: Rsa Encrypt a String with a Specific Private Key
Arkit - Raycasting Using a World Ray Instead of a Screen Point
Uitextview Contentoffset Is Set
Show and Hide Window Instead of Terminating App on Close Click in Cocoa App
Macos Security Scoped Url Bookmark for Folder
Swift 3 Google Map Add Markers on Touch
In Swift 3.1, Unsafemutablepointer.Initialize(From:) Is Deprecated
Swift Cannot Invoke '*' with an Argument List of Type '(Int, Int)'
Swift Playground with Debugger Support
Uitableview Only Displays One Section
Audiokit - Temporary File Size Is Too Big
Detecting End of The Playback in Avaudioplayer
How to Make Nsbutton Title at Bottom and Centered
Swiftui Text View Does Not Show Non-Displayable Characters
Xcode 13.1 Bug Editing Dependency as Local
iOS Swift, Aws Cognito User Pool, Unable to Refresh Access Token