Created
December 13, 2019 20:35
-
-
Save douglashill/884e0eacb42c5a3b7b359ac888cffaa2 to your computer and use it in GitHub Desktop.
The current file I use to smooth over API differences between AppKit and UIKit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Douglas Hill, April 2017 | |
// From https://douglashill.co/reading-app/ | |
#if os(macOS) | |
import AppKit | |
public typealias Colour = NSColor | |
public typealias Font = NSFont | |
public typealias FontDescriptor = NSFontDescriptor | |
public typealias Image = NSImage | |
public typealias KeyModifierFlags = NSEvent.ModifierFlags | |
let ItalicTrait = FontDescriptor.SymbolicTraits.italic | |
let BoldTrait = FontDescriptor.SymbolicTraits.bold | |
let optionKey = KeyModifierFlags.option | |
#elseif os(iOS) || os(tvOS) | |
import UIKit | |
public typealias Colour = UIColor | |
public typealias Font = UIFont | |
public typealias FontDescriptor = UIFontDescriptor | |
public typealias Image = UIImage | |
public typealias KeyModifierFlags = UIKeyModifierFlags | |
let ItalicTrait = FontDescriptor.SymbolicTraits.traitItalic | |
let BoldTrait = FontDescriptor.SymbolicTraits.traitBold | |
let optionKey = KeyModifierFlags.alternate | |
#endif | |
extension FontDescriptor { | |
// Force unwraps on iOS so the result is always not optional. Somewhat unsafe. Consider your usage. | |
// By the way, be very careful with this method. It discards important properties with certain inputs. | |
func dh_withSymbolicTraits(_ symbolicTraits: FontDescriptor.SymbolicTraits) -> FontDescriptor { | |
#if os(macOS) | |
return withSymbolicTraits(symbolicTraits) | |
#elseif os(iOS) || os(tvOS) | |
return withSymbolicTraits(symbolicTraits)! | |
#endif | |
} | |
} | |
extension Font { | |
// Force unwraps on Mac so the result is always not optional. Not sure when this returns nil. | |
convenience init(descriptor: FontDescriptor, overrideSize size: CGFloat = 0) { | |
#if os(macOS) | |
self.init(descriptor: descriptor, size: size)! | |
#elseif os(iOS) || os(tvOS) | |
self.init(descriptor: descriptor, size: size) | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment