Skip to content

Instantly share code, notes, and snippets.

@davidsteppenbeck
Created May 15, 2023 12:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsteppenbeck/e2967434e329c16aa07783e7ea0ed6a4 to your computer and use it in GitHub Desktop.
Save davidsteppenbeck/e2967434e329c16aa07783e7ea0ed6a4 to your computer and use it in GitHub Desktop.
A platform specific label for SwiftUI.
import SwiftUI
/// Provides a text label appropriate for the current platform.
///
/// Specifically, this provides a `Label` with the specified system image on iOS and `Text` without an image on macOS.
struct MultiplatformLabel: View {
// MARK:- Properties
/// A title generated from a localized string.
var titleKey: LocalizedStringKey
/// The name of the image resource to lookup.
var systemImage: String
var body: some View {
#if os(iOS)
Label(titleKey, systemImage: systemImage)
#elseif os(macOS)
Text(titleKey)
#endif
}
// MARK:- Initialization
init(_ titleKey: LocalizedStringKey, systemImage: String) {
self.titleKey = titleKey
self.systemImage = systemImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment