Dark mode test with custom typeface display.
First, follow this article from Apple to add the font file to your XCode project, and then register it with iOS.
Use the typeface like this:
Text("foobar")
.font(Font.custom("Condiment-Regular", size: 98))F3 in hexadecimal equals 0.95 in Double.
Example: #F3F1EC in HEX is UIColor(red:0.95, green:0.95, blue:0.93, alpha:1.0) in Swift.
Tips:
Using a tool like this, or Google hex to rgba to find a tool.
Use ZStack with color first show like a background.
ZStack {
Color.init(UIColor(red:0.95, green:0.95, blue:0.93, alpha:1.0))
.edgesIgnoringSafeArea(.all)
}.edgesIgnoringSafeArea(.all) means ignore safe area.
Adding dynamic colors in Assets.xcassets is recommend, fast and works great. Follow this article from Apple or this article.
Steps:
- Open
Assets.xcassets - Right-click the blank area of assets catalog then select
New Color Setto create a new color set - Select it then open Attribute Inspector (
⌘⌥4), and set its appearance fromNonetoAny, Dark - Select the appearance then define color you want. Set its input method from
Floating point (0.0–1.0)to8-bit Hexadecimalin Attribute Inspector if you want use HEX like#FFFFFF.
Use the Any Appearance variant to specify the color value to use on older systems that do not support Dark Mode.
I want to show "Light" in light appearance, and "Dark" in dark appearance.
Getting the \.colorScheme environment with: @Environment(\.colorScheme) var colorScheme: ColorScheme, then use it: Text(colorScheme == .light ? "Light" : "Dark").
Full code:
@Environment(\.colorScheme) var colorScheme: ColorScheme
var body: some View {
Text(colorScheme == .light ? "Light " : "Dark ")
.font(colorScheme == .light ? Font.custom("Condiment-Regular", size: 98) : Font.custom("Superclarendon", size: 78))
}