Created
May 15, 2023 12:13
-
-
Save davidsteppenbeck/e2967434e329c16aa07783e7ea0ed6a4 to your computer and use it in GitHub Desktop.
A platform specific label for SwiftUI.
This file contains hidden or 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
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