Post

Replies

Boosts

Views

Activity

Reply to Custom Fonts in Swift Package
I would like to report that as of this comment's writing, this approach of registering fonts after the app launches works only for SwiftUI apps, but not for UIKit apps which still use UIApplicationDelegate. For SwiftUI apps, it does suffice to define a register() function in your Swift Package, which you should then call in the SwiftUI app's init(), as follows: import MyCustomPackage @main struct SwiftUIDemoApp: App { init() { CustomFont.register() } } The original poster @dokun1 already has a working implementation of the register() function. However, UIKit apps seem to still require being configured as before, where font files must be in the application bundle and explicitly declared in the app target's Info.plist: https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app
Jul ’23
Reply to How to apply a SwiftUI view modifier to a container's subview?
I was able to do this using environment variables. I was initially confused because I found a StackOverflow answer that recommended the use of PreferenceKey, but the Apple Docs say that preference keys are for propagating values from a child view to its parent, while what I want to do is for a parent view to propagate a value down to a child, which is what environment values are for. I added the following to my custom container: struct CustomContainer: View { // ... // Define a new environment key. struct BorderStyle: EnvironmentKey { let color: Color let width: CGFloat static let defaultValue = BorderStyle(color: Color.gray, width: 1.2) } // Convenience view modifier for supplying values to the environment key. func border(_ color: Color, width: CGFloat = 1.2) -> some View { return environment(\.containerBorderStyle, BorderStyle(color: color, width: width)) } // Capture the environment key in a property of the parent view. @Environment(\.containerBorderStyle) private var borderStyle: BorderStyle var body: some View { ZStack { RoundedRectangle(cornerRadius: 8, style: .circular) .stroke(borderStyle.color, lineWidth: borderStyle.width) // use the environment key here // foreground here ... } } } And then I declared the custom environment key to an EnvironmentValues extension: extension EnvironmentValues { var containerBorderStyle: CustomContainer.BorderStyle { get { self[CustomContainer.BorderStyle.self] } set { self[CustomContainer.BorderStyle.self] = newValue } } }
Feb ’23
Reply to URLSessionDownloadTaskDelegate functions not called when using URLSession.download(for:), but works when using URLSession.downloadTask(with:)
Thank you @Scott, but I'm afraid that supplying the delegate to URLSession.download(for:delegate:) does not work, just as it does not when supplying the delegate in the URLSession initializer. At some point, I thought that maybe I needed to initialize the URLSession with a .background configuration instead of .default. However, when I did this, the line that calls URLSession.download(for:) throws an exception that exactly says: Completion handler blocks are not supported in background sessions. Use a delegate instead. I find this so bizarre since I am actually providing a delegate and I don't even have a way to provide a completion handler when calling the async function.
Jan ’23