Skip to content

Instantly share code, notes, and snippets.

View markiv's full-sized avatar

Vikram Kriplaney markiv

View GitHub Profile
@markiv
markiv / View+MeasureSize.swift
Created May 2, 2024 08:27
Measures the size of any view
import SwiftUI
struct MeasureSizeModifier: ViewModifier {
@Binding var size: CGSize
func body(content: Content) -> some View {
content.background {
GeometryReader { geometry in
Color.clear
.onAppear { size = geometry.size }
@markiv
markiv / String+EmojiFlag.swift
Created April 30, 2024 10:57
Swift extension that creates a Unicode flag emoji for the given ISO region/country code.
public extension String {
fileprivate static let unicodeFlagsBase = 0x1F1E6 - UInt32("A")
/// Creates a Unicode flag emoji for the given ISO region/country code.
///
/// String(flag: "CH") // returns "🇨🇭"
/// String(flag: "in") // returns "🇮🇳"
/// String(flag: "eu") // returns "🇪🇺"
/// String(flag: "42") // returns nil
///
// Allows us to persist any Codable with @AppStorage and @SceneStorage (via a JSON String).
public protocol RawCodable {
static var emptyJSON: String { get }
}
public extension RawCodable where Self: Codable {
init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let value = (try? JSONDecoder().decode(Self.self, from: data)) else { return nil }
public struct Backport<Portee> {
let portee: Portee
}
public extension URLSession {
var backport: Backport<URLSession> {
Backport(portee: self)
}
}
prefix operator ^
postfix operator ^
extension URL: ExpressibleByStringLiteral {
/// Conveniently initializes a `URL` from a string literal:
///
/// let url: URL = "https://www.somewhere.com"
public init(stringLiteral value: StringLiteralType) {
self.init(string: value)!
}
@markiv
markiv / DateHelpers.swift
Created January 29, 2022 15:26
Convenience initializers and helpers for Foundation Dates
import Foundation
extension Calendar {
static let gregorian = Calendar(identifier: .gregorian)
}
extension TimeZone {
static let utc = TimeZone(secondsFromGMT: 0)
}
@markiv
markiv / String+EmojiFlag.swift
Last active January 17, 2022 19:43
String extension to create emoji country and regional flags
extension String {
var flag: String {
uppercased().unicodeScalars
.filter { (65...90).contains($0.value) }
.compactMap { UnicodeScalar(0x1F1E6 - 65 + $0.value) }
.map(String.init)
.joined()
}
}
/*
If you've ever wished you could calculate or evaluate something inside your #SwiftUI view's body without repeating yourself, try this pseudo-view helper.
You can now compute multiple values in tuples, call helper functions or print debug expressions.
*/
struct Calculate<Content: View, Value>: View {
let calculation: () -> Value
let content: (Value) -> Content
init(
@markiv
markiv / FloatingTextFieldContentView.swift
Last active May 16, 2020 21:26
FloatingTextFieldContenView.swift
struct ContentView: View {
let labels = ["First Name", "Last Name", "Street", "City", "Post Code"]
@State private var values = Array(repeating: "", count: 5)
var body: some View {
List(0..<5) { index in
FloatingTextField(title: self.labels[index], text: self.$values[index])
}.listStyle(GroupedListStyle())
}
}
@markiv
markiv / FloatingTextField4.swift
Last active May 16, 2020 20:20
FloatingTextField4.swift
struct FloatingTextField: View {
let title: String
let text: Binding<String>
var body: some View {
ZStack(alignment: .leading) {
Text(title)
.foregroundColor(text.wrappedValue.isEmpty ? Color(.placeholderText) : .accentColor)
.offset(y: text.wrappedValue.isEmpty ? 0 : -25)
.scaleEffect(text.wrappedValue.isEmpty ? 1 : 0.75, anchor: .leading)