Skip to content

Instantly share code, notes, and snippets.

View davidsteppenbeck's full-sized avatar

David Steppenbeck davidsteppenbeck

View GitHub Profile
@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:
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 / 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 / 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 / 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
@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 / BindableExamples.swift
Created August 16, 2023 18:22
Examples of applying `Bindable` from environment observables in SwiftUI views in iOS 17
import SwiftUI
@Observable class ViewModel {
var isOn: Bool
var text: String
init(isOn: Bool = false, text: String = "default") {
self.isOn = isOn
self.text = text
}