Skip to content

Instantly share code, notes, and snippets.

View andresr-dev's full-sized avatar

Andres Raigoza andresr-dev

View GitHub Profile
@andresr-dev
andresr-dev / ios-font-sizes.swift
Created June 17, 2024 01:38 — forked from zacwest/ios-font-sizes.swift
iOS default font sizes - also available on https://www.iosfontsizes.com
let styles: [UIFont.TextStyle] = [
// iOS 17
.extraLargeTitle, .extraLargeTitle2,
// iOS 11
.largeTitle,
// iOS 9
.title1, .title2, .title3, .callout,
// iOS 7
.headline, .subheadline, .body, .footnote, .caption1, .caption2,
]
@andresr-dev
andresr-dev / DateFormatSelector.swift
Created August 13, 2023 17:25
This is a handy way to give format to your dates in swift
enum DateFormatSelector: String {
/// AM or PM only
case a = "a"
case MMMM = "MMMM"
case yyyy = "yyyy"
case Hmm = "H:mm"
/// The 24-hour hour padding with a zero if there is only 1 digit.
case HHmm = "HH:mm"
/// The 12-hour hour padding with a zero if there is only 1 digit
case hhmm = "hh:mm"
@andresr-dev
andresr-dev / RealmManager.swift
Created June 2, 2023 17:37
This is a Realm Manager class with all the CRUD operations in swift
import Foundation
import RealmSwift
class RealmManager {
/// Creates a `Realm` file in Documents directory
fileprivate var realm: Realm {
do {
return try Realm()
} catch let error {
let nsError = error as NSError
@andresr-dev
andresr-dev / UIApplication+WindowScence.swift
Created June 2, 2023 17:36
This is how to get the UIWindowScene in iOS
extension UIApplication {
var windowScene: UIWindowScene? {
guard let scene = connectedScenes.first(where: {
$0.activationState == .foregroundActive
}) else {
return nil
}
return scene as? UIWindowScene
}
}
@andresr-dev
andresr-dev / ProportionalZStack.swift
Created May 19, 2023 21:09
This is a handy container that allows you to specify a width proportion for its subviews
import SwiftUI
/// A view container that overlays its subviews with a given width proportion, aligning them in both axes.
///
/// The `ProportionalZStack` uses a width to specify how much of width proportion its subviews must take.
///
/// The following example creates a `ProportionalZStack` with a proposed width of 70%,
/// an aspectRatio of 4:3 and an offset of 80% for its subviews.
///
/// ```
@andresr-dev
andresr-dev / UIApplication+Window.swift
Last active June 2, 2023 17:34
This is how to get the current window in iOS
extension UIApplication {
var window: UIWindow? {
guard
let scene = connectedScenes.first,
let sceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let uiWindow = sceneDelegate.window
else {
return nil
}
return uiWindow
@andresr-dev
andresr-dev / DrawingView.swift
Last active May 5, 2023 17:07
This is a simple drawing view made with SwiftUI
import Foundation
import SwiftUI
struct SignatureLine {
var points = [CGPoint]()
let width = 3.0
}
struct DrawingView: View {
@State private var lines = [SignatureLine]()
@andresr-dev
andresr-dev / SwiftUIToBase64.swift
Last active May 5, 2023 17:35
This is how to convert a SwiftUI View to Base 64 format.
import SwiftUI
struct SwiftUIToBase64View: View {
@Environment(\.displayScale) var displayScale
var body: some View {
Text("Hello, World!")
}
@MainActor private func ecode() -> String? {
@andresr-dev
andresr-dev / TopViewController.swift
Last active May 5, 2023 16:41
This is how to get the top view controller in swift
var window: UIWindow? {
guard
let scene = UIApplication.shared.connectedScenes.first,
let sceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let uiWindow = sceneDelegate.window
else {
return nil
}
return uiWindow
}
@andresr-dev
andresr-dev / ScreenRecordingDetection.swift
Last active April 15, 2023 21:32
This is how to detect screen recording and snapshot events in iOS using SwiftUI
import SwiftUI
@main
struct ScreenCaptureBlockerApp: App {
@State private var hidingScreen = false
private var window: UIWindow? {
guard
let scene = UIApplication.shared.connectedScenes.first,
let sceneDelegate = scene.delegate as? UIWindowSceneDelegate,