Skip to content

Instantly share code, notes, and snippets.

View henrik-dmg's full-sized avatar

Henrik Panhans henrik-dmg

View GitHub Profile
@henrik-dmg
henrik-dmg / SignInWithAppleFirebaseButton.swift
Last active May 4, 2024 19:01
A SwiftUI wrapper around the new Sign in with Apple button that returns OAuthCredentials directly to login with Firebase
import AuthenticationServices
import CryptoKit
import FirebaseAuth
import Foundation
import SwiftUI
// Adapted from https://firebase.google.com/docs/auth/ios/apple?authuser=0
@available(iOS 14.0, OSX 10.16, tvOS 14.0, *)
@available(watchOS, unavailable)
@henrik-dmg
henrik-dmg / Size Preserving Aspect Ratio.swift
Created April 30, 2020 13:15
Fits the calling size into the provided while retaining aspec ratio
extension CGSize {
func sizeFittingPreservingAspectRatio(_ otherSize: CGSize) -> CGSize {
if height <= width {
let scaledWidth = (otherSize.height / height) * width
return CGSize(width: scaledWidth, height: otherSize.height)
} else {
let scaledHeight = (otherSize.width / width) * height
return CGSize(width: otherSize.width, height: scaledHeight)
}
@henrik-dmg
henrik-dmg / SystemFont.swift
Created November 25, 2019 19:15
A UIFont extension to enable dynamic for system font styles with any font weight
import Foundation
import UIKit
public extension UIFont {
static func preferredFont(forTextStyle style: UIFont.TextStyle, weight: UIFont.Weight) -> UIFont {
let font = style.withWeight(weight)
let fontMetrics = UIFontMetrics(forTextStyle: style)
return fontMetrics.scaledFont(for: font)
}
}
@henrik-dmg
henrik-dmg / ioslocaleidentifiers.csv
Last active July 18, 2019 13:49 — forked from jacobbubu/ioslocaleidentifiers.csv
iOS Locale Identifiers
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
af Afrikaans
af_NA Afrikaans (Namibia)
af_ZA Afrikaans (South Africa)
agq Aghem
agq_CM Aghem (Cameroon)
ak Akan
ak_GH Akan (Ghana)
am Amharic
am_ET Amharic (Ethiopia)
ar Arabic
extension UIView {
func roundCorners(_ corners: Corners, radius: CGFloat) {
var cornerMasks = [CACornerMask]()
// Top left corner
switch corners {
case .all, .top, .topLeft, .allButTopRight, .allButBottomLeft, .allButBottomRight, .topLeftBottomRight:
cornerMasks.append(CACornerMask(rawValue: UIRectCorner.topLeft.rawValue))
default:
break
@henrik-dmg
henrik-dmg / QRGenerator.swift
Created September 30, 2018 11:31
A function to generate a QR code from Data and then automatically scale it up to the targets size to remove blur
func generateQRCode(from data: Data) -> UIImage? {
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
guard let qrCodeImage = filter.outputImage else { return nil }
let scaleX = self.codeView.frame.size.width / qrCodeImage.extent.size.width
let scaleY = self.codeView.frame.size.height / qrCodeImage.extent.size.height
let transform = CGAffineTransform(scaleX: scaleX, y: scaleY)