Skip to content

Instantly share code, notes, and snippets.

@kirsteins
kirsteins / gist:62bd716dd03db68d402f5e6157b96e05
Created April 18, 2024 12:43
CustomLabelStyle that mimics List+Label
struct CustomLabelStyle: LabelStyle {
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
private var leadingPadding: CGFloat {
switch dynamicTypeSize {
case .xSmall: -8
case .small: -9
case .medium: -9
case .large: -10
case .xLarge: -11
@kirsteins
kirsteins / file.swift
Last active September 29, 2022 07:45
Rounding to decimal places with correct decimal seperator for locale
extension NSDecimalNumber {
func round(decimalPlaces: Int, roundingMode: RoundingMode) -> NSDecimalNumber {
let handler = NSDecimalNumberHandler(
roundingMode: roundingMode,
scale: Int16(decimalPlaces),
raiseOnExactness: false,
raiseOnOverflow: false,
raiseOnUnderflow: false,
raiseOnDivideByZero: false)
return rounding(accordingToBehavior: handler)
@kirsteins
kirsteins / gist:51bbb5f187987b3ec034096f265b68f2
Last active August 11, 2021 12:05
Toggles and labels aligned at fixed size
struct ToggleWidthPreferenceKey: PreferenceKey {
static let defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = max(value, nextValue())
}
}
extension View {
func measureToggle() -> some View {
self.background(GeometryReader { geometry in
@kirsteins
kirsteins / Transition.swift
Created February 4, 2021 09:37
Transitions in SwiftUI
struct ContentView: View {
@State var flag = false
var body: some View {
VStack(spacing: 0) {
Circle().fill(Color.orange)
if flag {
Circle()
.fill(Color.red)
.transition(.opacity)
struct ContentView: View {
@State var flag = false
var body: some View {
VStack(spacing: 0) {
Circle().fill(Color.orange)
if flag {
Circle()
.fill(Color.red)
.transition(.opacity)
@kirsteins
kirsteins / FR.alfredappearance
Last active March 22, 2019 14:16 — forked from frankrausch/FR.alfredappearance
My Alfred Appearance
{
"alfredtheme" : {
"result" : {
"textSpacing" : 4,
"subtext" : {
"size" : 12,
"colorSelected" : "#FFFFFFFF",
"font" : "CamingoCode",
"color" : "#7F7F7FFF"
},
@kirsteins
kirsteins / file.swift
Last active January 30, 2018 09:44
Wrapped Hashable
extension Optional where Wrapped: Hashable {
var hashValue: Int {
switch self {
case .some(let hashable):
return hashable.hashValue
default:
return 0
}
}
}
/// Get the top most view controller in presentation chain
func topPresentedController() -> UIViewController? {
var topPresentedController = rootViewController
while let nextTopPresentedController = topPresentedController?.presentedViewController {
topPresentedController = nextTopPresentedController
}
return topPresentedController
}
@kirsteins
kirsteins / ViewController.swift
Created March 11, 2016 17:37 — forked from kellyegan/ViewController.swift
Send emails with attachments in iOS using Swift
//
// ViewController.swift
// SendEmailWithAttachment
//
// Created by Kelly Egan on 3/17/15.
// Copyright (c) 2015 Kelly Egan. All rights reserved.
//
import UIKit
import MessageUI
import ObjectiveC
import UIKit
var associatedObjectHandle: UInt8 = 0
class TextDidChangeHandler: NSObject {
unowned let textField: UITextField
let handler: (textField: UITextField) -> Void
init(textField: UITextField, handler: (textField: UITextField) -> Void) {
self.handler = handler