Disabling Dynamic Type in Swift
Okay, first let me say this: while I am happy that I was able to quickly find a way to accommodate the dynamic text provided by the iOS accessibility settings (which I will show the code for in a sec) I think it is still important to get an answer to the original question.
That said, here is what I did to the table view code to respect the larger type that some users need. It was a two step process. First, add:
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
to the viewDidLoad method. Then, in the cellForRowAtIndexPath method, add the following before you return the cell:
cell.textLabel!.numberOfLines = 0
Good luck folks, and please add an answer to the original question if you have one :)
Fixing the size of a custom font in SwiftUI iOS 13+ when ignoring Dynamic Type
A small hack
It seems Apple does not give us a direct way to influence the max size of the dynamic fonts.
To avoid very small font sizes there is the minimumScaleFactor modifier like
.minimumScaleFactor(0.5)
To avoid very large fonts it is not so easy.
What I did in my app is to get the Environment variable called sizeCategory and depending on the value of this system variable, adjusting my font.
It should work for you as well. In your view, you would add the environment variable in your struct. Then if you know that the .accessibilityExtraExtraLarge
breaks your layout you can adjust it like this:
struct ContentView: View {
@Environment(\.sizeCategory) var sizeCategory
var body: some View {
Text("Hello World")
.font(sizeCategory == .accessibilityExtraExtraLarge ?
.custom("Helvetica", size: 23, relativeTo: .headline) :
.custom("Helvetica", size: 33, relativeTo: .headline))
.minimumScaleFactor(0.5)
}
}
I do not have your full code to test, but I would add this to your function it should work:
func getDigitalXFontOfSize(size s : CGFloat) -> Font {
if font(sizeCategory == .accessibilityExtraExtraLarge {
return Font.custom("DigitalX", size: s - 10)
} else {
return Font.custom("DigitalX", size: s)
}
I hope this would help. I guess using a switch statement inside the function could be an idea as well!
How to honor Dynamic Type Accessibility Sizes with a custom font in an iOS storyboard
Although you can't specify both a custom font and a preferred text style via Storyboard, it's not difficult to programmatically specify a dynamic type size for your custom font:
Swift:
let pointSize = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleTitle1).pointSize
let customFont = UIFont(name: "Chalkboard SE", size: pointSize)
When you receive a UIContentSizeCategoryDidChangeNotification
, use the same code to update your label's font.
Obj C:
CGFloat pointSize = [[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline] pointSize];
[titleLabel setFont:[UIFont fontWithName:@"Marker Felt" size:pointSize]];
Related Topics
Dismiss and Present View Controller in Swift
Nspredicate to Match "Any Entry in an Nsdatabase with Value That Contains a String"
Could Not Cast Value of Type 'Uiview' (0X112484Eb0) to 'Skview' (0X111646718)
Ios: Production Push Notifications, Invalid Token from Apns Server
How to Create a Uiimage with Uibezierpath
iOS Notification When Application Is Closed
Getting System Uptime in iOS/Swift
Apns Push Notifications Not Working on Production
Core Data Sum of All Instances Attribute
Fbsdkaccesstoken Currentaccesstoken Is Not Being Updated After Log In
Bounce Animation on Google Map Marker in iOS? [Objective-C]
How to Make Skscene Have Fixed Width
Determine Percentage of Visibility of Horizontal Collectionview Cells on Screen
Assertion Failure in Void _Uiperformresizeoftextviewfortextcontainer
How to Disable Meta Format-Detection on iOS for Dates
How to Change Device Orientation Programmatically in Swift
iOS Web App: Showing Content Only If the Application Is Standalone