Skip to content

Instantly share code, notes, and snippets.

@yannxou
yannxou / IsRunningOnSimulator.swift
Created January 24, 2022 16:42
Top-level property to find if code is running in simulator
#if targetEnvironment(simulator)
let isRunningOnSimulator = true
#else
let isRunningOnSimulator = false
#endif
@yannxou
yannxou / Deprecate function.swift
Last active November 24, 2021 10:32
Deprecate function #Swift #xcode
// From: https://www.avanderlee.com/swift/async-await/
// More info on @available: https://www.avanderlee.com/swift/available-deprecated-renamed/
@available(*, deprecated, renamed: "fetchImages()")
func fetchImages(completion: @escaping (Result<[UIImage], Error>) -> Void) { ... }
@yannxou
yannxou / ForegroundTextColor.swift
Created December 23, 2020 09:55
Foreground text color based on background color #SwiftUI
// Taken from Apple's App Dev Training: https://developer.apple.com/tutorials/app-dev-training/
/// This color is either black or white, whichever is more accessible when viewed against the scrum color.
var accessibleFontColor: Color {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
UIColor(self).getRed(&red, green: &green, blue: &blue, alpha: nil)
return isLightColor(red: red, green: green, blue: blue) ? .black : .white
}
@yannxou
yannxou / DeferSample.swift
Last active December 22, 2020 10:47
Defer example
struct MyType {
var property: String = "" {
didSet {
print("Property didSet: \(property)")
}
}
init(property: String) {
// self.property = property // this way didSet is not called on init
@yannxou
yannxou / AttributedText.swift
Last active November 9, 2020 16:57
AttributedText #SwiftUI
import SwiftUI
/// Note: Height is calculated dynamically and it is available only in run-time. To test it with Preview set a fixed size.
struct AttributedText: View {
@State private var textSize: CGSize = .zero
var attributedString: NSAttributedString
init(_ text: NSAttributedString) {
attributedString = text
}
@yannxou
yannxou / LoaderView.swift
Last active November 9, 2020 16:26
SwiftUI: LoaderView #SwiftUI
struct LoaderView<Content>: View where Content: View {
var isShowing: Bool
var content: () -> Content
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .center) {
self.content()
.disabled(self.isShowing)
.blur(radius: self.isShowing ? 8 : 0)
ActivityIndicator(
@yannxou
yannxou / SwiftUI: Computed properties as a way to improve clarity.swift
Last active November 9, 2020 16:26
Computed properties to clarify code #SwiftUI
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme: ColorScheme
var body: some View {
self.preferencesButton
}
var preferencesButton: some View {
Button(action: {
self.navigator.presenting = .preferences
}) {
Image(systemName: “selection.pin.in.out”)
@yannxou
yannxou / UILabel+lines.swift
Last active August 24, 2022 07:36
creates an array containing one entry for each line of text the label has
// Credit: https://stackoverflow.com/questions/4421267/how-to-get-text-string-from-nth-line-of-uilabel/43284779
extension UILabel {
/// creates an array containing one entry for each line of text the label has
var lines: [String]? {
guard let text = text, let font = font else { return nil }
let attStr = NSMutableAttributedString(string: text)
@yannxou
yannxou / SwiftUI Badge
Created April 22, 2020 15:13
SwiftUI Badge #SwiftUI
// code from the book “Thinking in SwiftUI” by Chris Eidhof
extension View {
func badge(count: Int) -> some View {
overlay(
ZStack {
if count != 0 {
Circle()
.fill(Color.red)
Text("\(count)")
@yannxou
yannxou / xcode: Fix SwiftUI canvas preview issues
Last active April 21, 2020 12:36
Fix SwiftUI canvas #xcode
1. xcrun simctl erase all
2. Restart computer!!