Skip to content

Instantly share code, notes, and snippets.

import SwiftUI
public extension Picker where SelectionValue: CaseIterable, SelectionValue.AllCases: RandomAccessCollection, SelectionValue: RawRepresentable, SelectionValue.RawValue == String {
init(_ title: some StringProtocol, selection: Binding<SelectionValue>) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> {
self.init(title, selection: selection) {
ForEach(SelectionValue.allCases, id: \.self) { element in
Text(element.rawValue)
}
}
}
@shaps80
shaps80 / Binding+OptionSet.swift
Created September 11, 2024 15:29
Simplifies binding option set values to a SwiftUI View.
import SwiftUI
public extension Binding where Value: OptionSet {
func toggling(_ value: Value.Element) -> Binding<Bool> {
.init(
get: {
wrappedValue.contains(value)
},
set: {
if $0 {
extension StringProtocol {
/// str[NSRange(location:0, length: 9)]
subscript(_ range: NSRange) -> SubSequence {
guard let stringRange = Range<String.Index>(range, in: self) else {
fatalError("String index is out of range")
}
return self[stringRange]
}
@Matt54
Matt54 / AnimatingMaskedMeshView.swift
Created June 13, 2024 12:21
An animating mesh gradient view with an animating mask
import SwiftUI
struct AnimatingMaskedMeshView: View {
let referenceDate: Date = .now
@State private var mainPosition: CGPoint = .zero
@State private var positions: [CGPoint] = []
private let blurRadius = 20.0
private let alphaThreshold = 0.875
@davidsteppenbeck
davidsteppenbeck / AnimatingMeshView.swift
Created June 11, 2024 16:41
Animating mesh gradient for iOS 18 using TimelineView
import SwiftUI
struct AnimatingMeshView: View {
let referenceDate: Date
var body: some View {
TimelineView(.animation) { context in
let t = context.date.timeIntervalSince(referenceDate)
MeshGradient(width: 5, height: 4, points: [
@byte-sourcerer
byte-sourcerer / MacEditorTextView.swift
Created October 31, 2021 10:46
An NSTextView wrapped by SwiftUI with TextKit 2
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://twitter.com/tholanda
*
* MIT license
* Modified by https://github.com/cjwcommuny for TextKit 2
*/
import Combine
@niw
niw / LICENSE
Last active May 14, 2023 15:35
macOS Big Sur Icon Template for Affinity Photo
macOS Big Sur Icon Template for Affinity Photo (c) by Yoshimasa Niwa
macOS Big Sur Icon Template for Affinity Photo is licensed under a
Creative Commons Attribution 4.0 International License.
You should have received a copy of the license along with this
work. If not, see <http://creativecommons.org/licenses/by/4.0/>.
@gaosa
gaosa / squircle.swift
Created December 21, 2019 12:09
Squircle in SwiftUI
// Run in Swift Playground
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
Rectangle()
.fill(Color.red)
.frame(width: 200, height: 200)
@unnamedd
unnamedd / MacEditorTextView.swift
Last active May 5, 2025 23:25
[SwiftUI] MacEditorTextView - A simple and small NSTextView wrapped by SwiftUI.
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://bsky.app/profile/tholanda.com
*
* (the twitter account is now deleted, please, do not try to reach me there)
* https://twitter.com/tholanda
*
* MIT license
*/
@codingmanu
codingmanu / TextExtension.swift
Last active June 20, 2019 21:39
SwiftUI Text coloring word initializer
// SwiftUI Text Initializer replacing occurences of a given word with a colored version.
// Made by Manuel Gomez (@codingManu) on 2019/06/19
extension Text {
public init<S>(_ content: S, making: S, _ color: Color) where S : StringProtocol {
let comps = content.components(separatedBy: making)
var baseText = Text("")
if comps[0] != making {