Skip to content

Instantly share code, notes, and snippets.

@globulus
globulus / EnhancedVideoPlayer.swift
Created Sep 12, 2021
Enhanced SwiftUI Video Player
View EnhancedVideoPlayer.swift
// Full recipe at https://swiftuirecipes.com/blog/play-video-in-swiftui
import SwiftUI
import AVKit
import Foundation
struct EnhancedVideoPlayer<VideoOverlay: View>: View {
@StateObject private var viewModel: ViewModel
@ViewBuilder var videoOverlay: () -> VideoOverlay
@globulus
globulus / SwipeActions.swift
Created Sep 2, 2021
Add custom row swipe actions to a SwiftUI List
View SwipeActions.swift
// Full recipe at https://swiftuirecipes.com/blog/swiftui-list-custom-row-swipe-actions-all-versions
import SwiftUI
// Button in swipe action, renders text or image and can have background color
struct SwipeActionButton: View, Identifiable {
static let width: CGFloat = 70
let id = UUID()
let text: Text?
@globulus
globulus / HyperlinkText.swift
Created Aug 26, 2021
SwiftUI Text with NSAttributedString, HTML or Markdown with tappable Hyperlinks
View HyperlinkText.swift
// full recipe at https://swiftuirecipes.com/blog/hyperlinks-in-swiftui-text
import SwiftUI
import SwiftUIFlowLayout
import MarkdownKit
struct HyperlinkTest: View {
var body: some View {
VStack {
HyperlinkText(html: "To <b>learn more</b>, <i>please</i> feel free to visit <a href=\"https://swiftuirecipes.com\">SwiftUIRecipes</a> for details, or check the <code>source code</code> at <a href=\"https://github.com/globulus\">Github page</a>.")
HyperlinkText(markdown: "To **learn more**, *please* feel free visit [SwiftUIRecipes](https://swiftuirecipes.com) for details, or check the `source code` at [Github page](https://github.com/globulus).")
View SwiftUIFreeformDrawing.swift
struct DrawingPath {
private var points = [CGPoint]()
private var breaks = [Int]()
mutating func addPoint(_ point: CGPoint) {
points.append(point)
}
mutating func addBreak() {
breaks.append(points.count)
@globulus
globulus / HTMLText.swift
Created May 27, 2021
SwiftUI Text with HTML via NSAttributedString
View HTMLText.swift
// full recipe at https://swiftuirecipes.com/blog/swiftui-text-with-html-via-nsattributedstring
extension Text {
init(html htmlString: String,
raw: Bool = false,
size: CGFloat? = nil,
fontFamily: String = "-apple-system") {
let fullHTML: String
if raw {
fullHTML = htmlString
} else {
@globulus
globulus / DIFramework.swift
Created Dec 16, 2020
Basic DI framework in Swift
View DIFramework.swift
import Foundation
public struct Service {
public enum LifeCycle {
case singleton
case factory
}
/// Holds the lifecycle of the current service
public var cycle: LifeCycle