until now I have been using fixed font sizes, like this:
Text("Example")
.font(.system(size: 24 weight: .medium, design: .rounded))
I am now trying to implement dynamic font sizes, so it scales with different accessibility modes. I found this, which says "If you’re using iOS 14 or later you’ll find your custom fonts scale automatically with no further work from you". This does work for me, but now I can't find a way to make the font .rounded (as .fontDesign is iOS 16+, and I am targeting iOS 15).
Also, what should I use as the first parameter (font name) for the system font? I tried UIFont.systemFont, etc, which didn't work. I'm not even 100% sure SF Pro is the correct name here:
Text("Example")
.font(.custom("SF Pro", size: 24))
Perhaps I'm doing this completely wrong in some other way that is not clear to me. Is this the correct approach to dynamic font sizing for accessibility — to use .custom, specify a default size (for the default type size), and let iOS scale this accordingly?
I realised that if I remove the .weight parameter and use .largeTitle, etc, it works — and I can set the weight separately:
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.bold)
How do I set the default sizes of .largeTitle, .body, etc?
Related
In UIKit I was able to access a custom font like this
guard let font = UIFont(name: "OpenSans-Regular", size: 12) else {
fatalError("Font not available")
}
In SwiftUI we get a custom font like this.
//Swift UI Version
let font = Font.custom("OpenSans-Regular", size: 12)
The issue seems to be that if you have not "registered" the font in the UIKit version you would get the fatal error. Which is fine as you could just register the font at that point if needed.
The SwiftUI version seems to just return the system SF font if the font is not registered.
I would like to know a way to check if a font has been registered before I access it view SwiftUI.
UIFont exists in SwiftUI as well. Check the value, and set a #State value based on the result. Font has a completely different use; it's always is going to resolve to something. UIFont didn't go away with the release of SwiftUI (nor did CoreText or a many other tools).
I am struggling with a basic beginners problem at the moment. My app should run on all the different iPhones. My problem right now is that my constraints are not really responsive.
Examples:
As you can see on the iPhone 11 everything looks perfectly fine but on the iPhone 6s it a bit messed up sometimes.
My question, is there an easy way to make my design responsive? Not just the constraints but also text (e.g. "Main Wishlist" in the 3rd picture).
By the way I am doing everything programmatically. Any help on this is appreciated :)
Using the Dynamic Type you can solve the dynamic font problem
Here is an example of how you can scale your Fonts Automatically based on the content size using the Dynamic Type:
guard let customFont = UIFont(name: "CustomFont-Light", size: UIFont.labelFontSize) else {
fatalError("""
Failed to load the "CustomFont-Light" font.
Make sure the font file is included in the project and the font name is spelled correctly.
"""
)
}
label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: customFont)
label.adjustsFontForContentSizeCategory = true
https://developer.apple.com/documentation/uikit/uifont/scaling_fonts_automatically
Using the Auto Shrink Property
follow this accepted answer
Scale text label by screen size
I am attempting to use a system font and apply the monospaced design, without luck. I can successfully make the text monospaced using the custom font function and passing in Courier and a size, but this is not idea because then the font size is fixed.
VStack {
Text("lmlmlmlm 12345678")
Text("lmlmlmlm 12345678")
.font(Font.system(.body, design: .monospaced))
Text("lmlmlmlm 12345678")
.font(Font.custom("Courier", size: 18))
}
How do I get the system font to work with the .monospaced design? I think it might be a bug with .monospaced, because the .serif option does modify the text as expected.
It seems .monospaced font only applies when given a fixed size:
Text("monospaced")
.font(.system(size: 14, design: .monospaced))
This won't work given a dynamic text style such as body. But as you've also mentioned it works fine for other fonts so this is probably a bug in Xcode 11.0 beta and hopefully will be fixed in next releases.
Update:
This issue was fixed with Xcode 11 beta 3. The following code works now:
Text("monospaced")
.font(.system(.body, design: .monospaced))
In case you want to make only digits monospaced, you might try using something like this:
Text("0123456789")
.font(Font.system(.body, design: .monospaced).monospacedDigit())
This does not circumvent the obvious bug of Xcode 11.0 beta, however. Letters are still not rendered monospaced.
I want to make an app, where font size increase or decrease according to font size maintained in iOS in Settings. If you change font size of your iOS from settings, whatsapp font size displayed accordingly, I want same functionality.
The iOS functionality you're looking for is the Dynamic Type that only works for text with implemented text styles.
Basically, you must :
Use the text styles but beware, their availability depends on your iOS version.
Tick the adjustsFontForContentSizeCategory property (since iOS 10) in your interface builder or implement it in code so as to tell the system to handle automatically the Dynamic Type for the object it belongs to (text styles must be used of course).
Adapt all your constraints to the different sizes your app may encounter.
You can also follow the notifications related to the Dynamic Typeevents as indicated below :
Everything is well explained in this WWDC video detailed summary where all the contents and their video timelapses are indicated to reach rapidly the information.
There's also the possibility of adapting the graphical elements size as well with a Dynamic Type implementation.
All you have to do if to use Dynamic Type for your labels. This means not to set it explicitly but to use styles like Header 1 or caption. This styles are depends on user setting in Accessibility and will change automatically. https://www.raywenderlich.com/77092/text-kit-tutorial-swift
You can use system default sizes for Texts, like,
self.label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
Please find documentation here.
For other components you can use like labelFontSize() and many more,
+ (CGFloat)labelFontSize;//Returns the standard font size used for labels.
+ (CGFloat)buttonFontSize;//Returns the standard font size used for buttons.
+ (CGFloat)smallSystemFontSize;//Returns the size of the standard small system font.
+ (CGFloat)systemFontSize;//Returns the size of the standard system font.
I try to clone a placeholder behavior for a tableCell. It must look exactly like the placeholder used within a UITextField. Does anybody know which Font is used by Apple as well as which Font size?
Thanks
The default font in UITextfield is HelveticaNeue 14