Minor Wasted Time Update Submitted

Since completely rewriting WastedTime into SwiftUI and fixing it to run nearly the same code across macOS, watchOS, iOS, iPadOS, and tvOS, I’ve not been happy with the way I’ve handled the fonts.

Today I took a step back and made changes to better handle the needs of larger devices.

Ultimately the code is pretty easy. First I added a new computed property on most screens. This computed property works on all platforms EXCEPT for watchOS. Here it is –

var isIpad: Bool {
    #if !os(watchOS)
        UIDevice.current.userInterfaceIdiom == .pad
    #else
        false
    #endif
}

All this does is define a simple boolean indicating if you are on a iPad like device. The reason there is a compiler directive looking for watchOS is that UIDevice is not available in watchOS. Luckily, we can just set the isIPad to false if we are on the watch.

Second, I define a new extension on SwiftUI’s Font with two computed properties:

extension Font {
    static var systemSpecificBody: Font {
        #if !targetEnvironment(macCatalyst)
        #if os(iOS) || os(watchOS) || os(tvOS)
            .body
        #else
            .title
        #endif
        #else
            .title
        #endif
    }

    static var iPadSpecificBody: Font {
        .title
    }
}

These two computed properties are used to return the font size I’d like to use for text. The reason I have two is so that I can use my previously defined boolean (isIPad) to set the font. Again we uncover a difference between Apple’s platforms. there is no compiler directive to say you are compiling for iPadOS. iPadOS is evidently more just marketing, and not really an operating system.

Additionally, since I am using macCatalyst for my macOS version of WastedTime, I have to capture the targetEnvironment to recognize that I will be running on macOS.

Having done both of these preparatory steps, I can now easily set the font for any text in my SwiftUI views. I do this with following code:

.font(isIpad ? .iPadSpecificBody : .systemSpecificBody)

This is a ternary, basically an inline if-else statement. The means set the value of .font to .iPadSpecificBody if the value of isIPad is true, otherwise use .systemSpecificBody.

And that’s it. I now correctly increase the fonts from .body to .title for those devices that have more space.

Tagged , . Bookmark the permalink.

Comments are closed.