Skip to content

Instantly share code, notes, and snippets.

View davidsteppenbeck's full-sized avatar

David Steppenbeck davidsteppenbeck

View GitHub Profile
@davidsteppenbeck
davidsteppenbeck / PreviewProviderModifier.swift
Last active October 31, 2022 10:30
A SwiftUI view modifier for simple preview providers.
import SwiftUI
enum PreviewProviderMode: CaseIterable {
/// Use for a light appearance preview.
case lightMode
/// Use for a dark appearance preview.
case darkMode
@davidsteppenbeck
davidsteppenbeck / ColorIconLabelStyle.swift
Created October 22, 2020 12:32
A SwiftUI label style for icons with dynamic colored backgrounds.
/// Sets a colored rounded rectangle behind the icon.
struct ColorIconLabelStyle: LabelStyle {
/// The color behind the icon.
var color: Color
/// The corner radius of the color behind the icon.
@ScaledMetric private var radius: CGFloat = 5
func makeBody(configuration: Configuration) -> some View {
@davidsteppenbeck
davidsteppenbeck / ContentSizeCategoryModifier.swift
Created May 7, 2021 17:09
A SwiftUI view modifier to limit the content size category of a view to a specified maximum size.
import SwiftUI
fileprivate struct ContentSizeCategoryModifier: ViewModifier {
// MARK:- Properties
/// The preferred size of the content.
@Environment(\.sizeCategory) private var sizeCategory
/// The maximum permitted content size category.
@davidsteppenbeck
davidsteppenbeck / Array+Utilities.swift
Last active May 17, 2021 20:52
Array extension method for safe index queries (Swift).
import Foundation
extension Array {
/// Safely provides the element at the requested index without encountering those dreaded index out of range errors 🥳.
///
/// Take, for example, an array of characters.
/// ````
/// let characters: [Character] = ["a", "b", "c", "d", "e"]
/// ````
@davidsteppenbeck
davidsteppenbeck / Optional+Utilities.swift
Last active July 27, 2021 07:08
An infix operator for collections that returns the wrapped value of the optional only if the collection is not empty.
import Foundation
infix operator ???
extension Optional where Wrapped: Collection {
/// Performs a nil-coalescing operation, returning the wrapped value of an `Optional` instance
/// only if the wrapped value is not empty, otherwise returns a default value.
///
/// - Parameters:
@davidsteppenbeck
davidsteppenbeck / ColorText.swift
Last active May 5, 2023 05:26
Attributed strings in Swift: Add color to strings directly in your Localizable.strings files.
import SwiftUI
enum ColorAttribute: CodableAttributedStringKey, MarkdownDecodableAttributedStringKey {
enum Value: String, Codable, Hashable {
case red
case orange
case yellow
case green
case mint
case teal
import SwiftUI
import WidgetKit
enum WidgetPreviewProviderMode: CaseIterable {
/// Use for a system small widget preview.
case small
/// Use for a system medium widget preview.
case medium
@davidsteppenbeck
davidsteppenbeck / PropertyWrapperApp.swift
Last active January 30, 2023 10:15
A Swift property wrapper for storing colors in User Defaults.
import SwiftUI
@main
struct PropertyWrapperApp: App {
var body: some Scene {
WindowGroup {
VStack {
ContentView()
ContentView()
}
@davidsteppenbeck
davidsteppenbeck / SymbolText.swift
Last active May 5, 2023 05:26
Attributed strings in Swift: Add colored SFSymbols to strings directly in your Localizable.strings files.
import SwiftUI
enum ColorAttribute: CodableAttributedStringKey, MarkdownDecodableAttributedStringKey {
enum Value: String, Codable, Hashable {
case red
case orange
case yellow
case green
case mint
case teal
@davidsteppenbeck
davidsteppenbeck / ValueForPlatform.swift
Last active December 25, 2023 22:27
A global function for providing multiplatform specific values in Swift.
import Foundation
/// Returns the value for the current platform from the values that are provided.
///
/// - Parameters:
/// - iOSValue: The value to use for the iOS platform.
/// - macOSValue: The value to use for the macOS platform.
func valueForPlatform<Value>(iOS iOSValue: @autoclosure () -> Value, macOS macOSValue: @autoclosure () -> Value) -> Value {
#if os(iOS)
iOSValue()