Last active
September 25, 2021 00:02
-
-
Save GeekAndDad/47f94f0e4fb862bdea8b5efe5dffab07 to your computer and use it in GitHub Desktop.
From the blog post "Tiny Hints: a couple of Xcode 11.3 SwiftUI Preview tips"
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
// | |
// ContentView.swift | |
// PreviewsTips2020.01.24 | |
// Updated 2021.09.24 | |
// | |
// Dad | |
// @GeekAndDad | |
// | |
// Code from blog post: | |
// https://geekanddad.wordpress.com/2020/01/24/tiny-hints-a-couple-of-xcode-11-3-swiftui-preview-tips/ | |
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
HStack { | |
Image(systemName: "paperplane") | |
.font(.largeTitle) | |
VStack { | |
Text("Title Text") | |
.font(.title) | |
Text("Subtitle goes here") | |
.font(.subheadline) | |
} | |
} | |
} | |
} | |
struct Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
.previewLayout(.fixed(width: 400, height: 100)) | |
} | |
} | |
// showing the limitation of Dark Mode previews when not in a | |
// NavigationView | |
struct PreviewsDarkMode: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
.previewLayout(.fixed(width: 400, height: 100)) | |
.border(Color.green) | |
.preferredColorScheme(.dark) | |
} | |
} | |
struct MyPreviewModifer: ViewModifier { | |
var displayMode: ColorScheme = .light | |
var name: String = "" | |
func body(content: Content) -> some View { | |
content | |
.previewLayout(.fixed(width: 400, height: 120)) | |
.previewDisplayName(name) | |
.preferredColorScheme(displayMode) | |
} | |
} | |
struct PreviewsLightDarkDynamicText: PreviewProvider { | |
static var previews: some View { | |
Group { | |
ContentView() | |
.modifier(MyPreviewModifer()) | |
ContentView() | |
.modifier(MyPreviewModifer(displayMode: .dark)) | |
ContentView() | |
.modifier(MyPreviewModifer(displayMode: .dark, name: "dark extraSmall")) | |
.environment(\.sizeCategory, .extraSmall) | |
ContentView() | |
.modifier(MyPreviewModifer(displayMode: .dark, name: "dark extraExtraLarge")) | |
.environment(\.sizeCategory, .extraExtraLarge) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog post motivating this and with screenshots is here.