Fixed font size in iOS 14 widget with SwiftUI - swiftui

For an iOS 14 widget I want to fix the font size in the widget, even when a user has its settings to bigger type in accessibility settings (larger type). I want this because the text is already very big (big digital clock widget), and if it is even bigger the text falls of the widget and therefore is not legible. I am using a custom font.
I tried to fix the font size with .font(.custom("MyCustomFont", size: 17)). But even then the type is rendered bigger depending on user settings. How can I permanently fix font size in a iOS 14 widget? Is there a workaround or another method to fix font size for the widget?

You can use .environment(\.sizeCategory, .large) to enforce the size category. Here is an example:
var body: some View {
VStack {
Text("text1")
Text("text2")
}
.environment(\.sizeCategory, .large)
}
The default category is large but here you can find more categories.

Related

swiftui picker how to hide up down arrows (ios16)

Is there a good way to hide the up down arrows in the picker default style. I am using ios 16. It seems that the older version does not have such arrows.
Also, is there a setting to set the picker's background to the same style as the datepicker in the image without manually setting the background and radious?
I have been struggling on this small feature and tried googling for a few hours but no luck. Any idea will be appreciated
The suggested Menu solution works well if you only have a few options. The problem I've experienced with the Menu solution is that if there are very many options the Menu doesn't automatically scroll to the currently selected option the way the Picker does.
The solution I've used is to use ZStack to place an opaque picker on top of a custom view (my "label"). Setting the opacity modifier on the Picker to 0.025 makes it invisible on your device but it will still trigger when you tap it.
This way you get all the native functionality of the Picker (including scrolling to the selected option) and you can make the label look any way you want without having to create your own custom picker.
Here's the code:
ZStack {
// Custom picker label
Text("\(value)")
.font(.title)
.foregroundColor(.blue)
.styleDataEntry(colorScheme: colorScheme) // a custom formatter View extension
// Invisible picker
Picker("", selection: $value) {
ForEach(0 ..< 200) { option in
Text("\(option)").tag(option)
}
}
.pickerStyle(.menu)
.opacity(0.025)
}

SwiftUI System Font how to override

I want to change the default font used in SwiftUI to Ariel.
Far as I can tell its embedded
I know I can set like
Text("Hello, world!").font(.custom(
"AmericanTypewriter", fixedSize: 36))
but I have to do this on every label and loose the automatic size stuff like
.font(.title)
Is there a way to set the system font globally

Change Title Bar Colour SwiftUI (No NavigationView nor .ignoresSafeArea())

There are two solutions I have seen can be used to change the colour of the title bar in SwiftUI:
Making the use of NavigationView
Ignoring the sage area with .ignoresSafeArea()
I have no use for a NavigationView nor I want to use .ignoresSafeArea() since it helps on giving the TabView bottom tab space.
The only requirement to the solution I am looking for is that it cannot be for iOS 16 and above only, since it will not be viable to use in production for at least the next two years.
TL;DR:
I need to change the colour of the title bar (the upper-most part of the app) with features available in versions prior to iOS 16, and without the use of NavigationView and .ignoresSafeArea()
The white stuff in this image:

How to determine width needed for a button in SwiftUI

If I have a string that I will place inside of a SwiftUI button label, how can I determine how much width I need to allow the button to consume such that the text in the button will not be truncated with ellipsis?
This would assume no font size modifier is applied and would take into account the current Dynamic Type size preference.
The fixedSize modifier as mentioned by Asperi works for this.

SwiftUI small widget is not opening the iOS app on tap

In my widget, I place a header view.
var header: some View {
ZStack {
Link(destination: url) {
Rectangle().fill(Color("WidgetBackground")).frame(height: 36)
Text("Header")
}
}
}
This is a simple text on a colored background.
The url is the urlscheme for my app.
When I tap on the label or on header view, it opens my app on medium and large size widgets.
But on the small size widgets, it didnt open the app, but it flickers and reload the widget.
I also see the flicker happen in medium and large size widgets when I tap on empty spaces other than elements.
I have no clue , why this flicker happens.
Based on documentation, interacting on widget should open the app, without any extra effort.
Am I missing something ?
I don’t know why the app doesn’t open (an iOS bug, I guess), but your deeplink won’t work because Link isn’t available in the “small” widget size. You need to set the .widgetURL() modifier on your view instead.