Skip to content

Instantly share code, notes, and snippets.

View Joony's full-sized avatar
🏠
Working from home

Jonathan McAllister Joony

🏠
Working from home
  • Copenhagen, Denmark
View GitHub Profile
@Joony
Joony / View+WithoutAnimation.swift
Created October 7, 2022 11:25
SwiftUI, change data without triggering an animation, for example: show a fullscreencover.
extension View {
func withoutAnimation(action: @escaping () -> Void) {
var transaction = Transaction()
transaction.disablesAnimations = true
withTransaction(transaction) {
action()
}
}
}
@Joony
Joony / DateFormatting.swift
Created September 13, 2022 08:40
Convert a date to a text representation, for example "now", "yesterday", "3 months ago"
let formatter = ISO8601DateFormatter()
// formatter.locale = Locale(identifier: "ko_KR")
let date = formatter.date(from: "2022-06-14T10:44:00+0000")!
date.formatted(.relative(presentation: .named)) // "yesterday", "2 days ago", "last week", "next week"
@Joony
Joony / NameFormatting.swift
Created March 23, 2022 13:07
How to properly format a person's name.
let formatter = PersonNameComponentsFormatter()
// formatter.locale = Locale(identifier: "ko_KR")
var components = PersonNameComponents()
components.familyName = "Doe"
components.givenName = "John"
formatter.string(from: components) // "John Doe" for "en_GB" locale, but "Doe John" for "ko_KR" locale.
@Joony
Joony / HeightFormat.swift
Created March 16, 2022 14:02
Convert between different units and always format the value for the user's locale. Example is storing the height in cm and showing the value in feet and inches.
let formatter = LengthFormatter()
formatter.numberFormatter.locale = Locale(identifier: "en_US") // Forcing a locale here just as an example.
formatter.numberFormatter.maximumFractionDigits = 0
formatter.isForPersonHeightUse = true
let height = Measurement<UnitLength>(value: 190, unit: .centimeters) // Use whichever unit you like here.
let meters = height.converted(to: .meters)
formatter.string(fromMeters: meters.value) // "6 ft, 3 in"
@Joony
Joony / #RoundedCorners-Example.swift
Created March 16, 2022 08:39
A View Modifier for rounding specific corners of a view.
import SwiftUI
struct RoundedCornerTestView: View {
var body: some View {
Rectangle()
.frame(width: 100, height: 100)
.cornerRadius(12, corners: [.bottomLeft, .bottomRight])
.foregroundColor(Color.red)
}
}
@Joony
Joony / #L10n-Example.swift
Created March 15, 2022 15:52
An easy way to handle dynamic localisation changes from within the app.
import SwiftUI
struct L10nTestView: View {
@EnvironmentObject var l10n: L10n
let titleKey = "title_key"
let nameKey = "name_key_%@"
var body: some View {
VStack {
@Joony
Joony / UIKitView-Example.swift
Created March 15, 2022 14:45
A SwiftUI wrapper for UIKit views.
import SwiftUI
struct TestView: View {
var body: some View {
UIKitView<UILabel> {
$0.numberOfLines = 0
$0.lineBreakMode = .byWordWrapping
$0.preferredMaxLayoutWidth = 340
$0.text = "Test UIKitView UILabel"
}
@Joony
Joony / #ScenePhaseModifier-Example.swift
Created March 15, 2022 14:30
A SwiftUI View Modifier to easily listen for scene changes.
.onScenePhaseChange(phase: .background) {
// Do something when the app enters the background.
}
@Joony
Joony / BuildInfo.swift
Last active April 18, 2024 13:41
Some useful information/metadata about the current build. Version number, build number, bundle identifier, is TestFlight, build date, installation date, App Store version, App Store app Id, settings URL, subscriptions URL, and App Store URL.
/// The build number of the app.
var buildNumber: Int {
Int(Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "0")!
}
/// The version string of the app, for example: "1.0.2"
var versionNumber: String {
Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
}
@Joony
Joony / #AppStorageObject-Example.swift
Created March 10, 2022 14:15
An example of how to save an array in AppStorage (SwiftUI)
@AppStorage("saved_array") var savedArray = ArrayTypeToSave()
savedArray = ["Hello", "World"]