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 / LocalizableMarkdown.swift
Created January 30, 2023 00:24
This is how to use markdown on your localizable strings in swift
// Localizable.string - Spanish
"Welcome **%@**, _please visit our [website](http://wwww.example.com)_" =
"Bienvenido **%@**, _visita nuestro [sitioweb](http://wwww.example.com)_";
let welcomeAttributedString =
AttributedString(localized: "Welcome **\(userName)**, _please visit our [website](http://wwww.example.com)_"
// Source: https://medium.com/geekculture/automatic-grammar-agreement-b953cbf5d101
@andresr-dev
andresr-dev / DateFormatters.txt
Created January 16, 2023 16:46
This is some of the dates formatters that you can use with DateFormatter().format in swift
Wednesday, Sep 12, 2018 --> EEEE, MMM d, yyyy
09/12/2018 --> MM/dd/yyyy
09-12-2018 14:11 --> MM-dd-yyyy HH:mm
Sep 12, 2:11 PM --> MMM d, h:mm a
Sep 12, 2022 14:11 --> MMM d, yyyy HH:mm
September 2018 --> MMMM yyyy
Sep 12, 2018 --> MMM d, yyyy
Wed, 12 Sep 2018 14:11:54 +0000 --> E, d MMM yyyy HH:mm:ss Z
2018-09-12T14:11:54+0000 --> yyyy-MM-dd'T'HH:mm:ssZ
12.09.18 --> dd.MM.yy
@andresr-dev
andresr-dev / ScrollViewNoBounce.swift
Created January 16, 2023 15:51
This is how to stop a ScrollView from bouncing in SwiftUI
import SwiftUI
struct ScrollViewNoBounce: View {
var body: some View {
ScrollView {
Text("Hello")
}
.onAppear {
UIScrollView.appearance().bounces = false
}
@andresr-dev
andresr-dev / ScrollViewReader.swift
Created January 12, 2023 13:29
This is how to scroll programmatically in SwiftUI
import SwiftUI
struct SwiftUIView: View {
@Namespace var topID
@Namespace var bottomID
var body: some View {
ScrollViewReader { proxy in
ScrollView {
VStack(spacing: 15) {
@andresr-dev
andresr-dev / PropertyWrapper-UserDefaults.swift
Created January 9, 2023 18:12
This is an example of how to create a property wrapper that automatically saves values to UserDefaults.
import Foundation
@propertyWrapper
struct UserDefaultsBacked<Value> {
private let key: String
let storage: UserDefaults = .standard
let defaultValue: Value
var wrappedValue: Value {
get {
@andresr-dev
andresr-dev / PropertyWrapper-Capitalized.swift
Created January 9, 2023 18:07
This is an example of how to use the propertyWrapper attribute to autocapitalize strings in swift
@propertyWrapper
struct Capitalized {
var wrappedValue: String {
didSet { wrappedValue = wrappedValue.capitalized }
}
init(wrappedValue: String) {
// we need to explicitly capitalize any string that was passed into our initializer
// since property observers are only triggered after a value or object was fully initialized.
self.wrappedValue = wrappedValue.capitalized
@andresr-dev
andresr-dev / ListRowSeparatorAlignment.swift
Last active January 8, 2023 23:46
This is how to align a list row separator using HorizontalAlignment.listRowSeparatorLeading in SwiftUI
import SwiftUI
struct Fruit: Identifiable {
let id = UUID()
let name: String
let emoji: String
static var examples: [Fruit] {
[
Fruit(name: "Kiwi", emoji: "🥝"),
@andresr-dev
andresr-dev / UnwrapOrThrow.swift
Last active January 8, 2023 23:34
This is an example of how to use @autoclosure, note that we don't need to use braces when we use autoclosures as arguments
import Foundation
extension Optional {
func unwrapOrThrow(_ errorExpression: @autoclosure () -> Error) throws -> Wrapped {
guard let value = self else {
throw errorExpression()
}
return value
}
@andresr-dev
andresr-dev / ContainerRelativeShapeExample.swift
Created January 7, 2023 22:32
This is how to use View.containerShape() and ContainerRelativeShape() in SwiftUI
import SwiftUI
struct PlatterContainer<Content: View>: View {
@ViewBuilder let content: Content
let shape = RoundedRectangle(cornerRadius: 20)
var body: some View {
content
.frame(width: 100, height: 100)
@andresr-dev
andresr-dev / ConditionalViewModifier.swift
Last active January 7, 2023 15:33
This is how to apply a conditional view modifier in SwiftUI
import SwiftUI
// Source: https://www.avanderlee.com/swiftui/conditional-view-modifier/
extension View {
@ViewBuilder func `if`<Content: View>(_ condition: Bool, apply: (Self) -> Content) -> some View {
if condition {
apply(self)
} else {
self