Skip to content

Instantly share code, notes, and snippets.

View davidsteppenbeck's full-sized avatar

David Steppenbeck davidsteppenbeck

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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