Skip to content

Instantly share code, notes, and snippets.

View lalkrishna's full-sized avatar
🤩

Lal Krishna lalkrishna

🤩
View GitHub Profile
//
// LocalAuthorization.swift
// LocalAuthorization
//
// Created by lalkrishna.
//
// Copyright © 2020 Lal Krishna. All rights reserved.
//
import Foundation
@lalkrishna
lalkrishna / Fonts.swift
Created September 13, 2020 13:18
Make use of custom fonts with the help of extensions. Note: iOS not detecting `black` font weight for Montserrat font. Please comment if you know the solution for it.
// MARK: - Usage
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UIFont.registerAllCustomFonts()
// Or You can add manually,
// https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app
return true
}
// Using Default family name
import UIKit
class SelfSizedTableView: UITableView {
var maxHeight: CGFloat = UIScreen.main.bounds.size.height
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
self.layoutIfNeeded()
}
@lalkrishna
lalkrishna / universalFrameworkGuide.md
Last active December 16, 2020 19:44
Creating Universal Framework with Bitcode support
@lalkrishna
lalkrishna / xcframework_generate.md
Created December 16, 2020 19:42
Create xcframework instead of Universal(fat) frameworks.

Advantages of xcframework

  • Support for multiple platforms in the same framework.
  • Support for Swift, Objective-C and C.
  • Support for dynamic frameworks and static libraries.
  • Support for Swift Package Manager.
  • End of fat binaries.

Steps to create Aggregate target:

  1. Open Current framework project
@lalkrishna
lalkrishna / AppContainer.swift
Created January 26, 2021 16:05
Dependency injection using Property wrappers (like @EnvironmentObject in SwiftUI). Core code from: https://medium.com/@anuragajwani/dependency-injection-in-ios-and-swift-using-property-wrappers-f411117cfdcf
/* SET */
Container.shared.register(type: User.self, User())
Container.shared.set(User(), forKey: "SecondUser")
/* GET */
let sameUser = Container.shared.resolve(User.self)
let secondUser = Container.shared.get("SecondUser", of: User.self)
/* REMOVE */
$user.deregister() // This will remove "User" from factory.
protocol UnknownCase: RawRepresentable, CaseIterable where RawValue: Equatable & Codable {
static var unknownCase: Self { get }
}
extension UnknownCase {
init(rawValue: RawValue) {
let value = Self.allCases.first { $0.rawValue == rawValue }
self = value ?? Self.unknownCase
}
@lalkrishna
lalkrishna / throttledSearch.swift
Created August 30, 2021 12:15
Throttled / Incremental search on Swift
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard !searchText.isEmpty else { searchQuery(); return }
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(searchQuery), object: nil)
perform(#selector(searchQuery), with: nil, afterDelay: 0.4)
}
@objc func searchQuery() {
guard let searchText = searchBar.text else { return }
// Call API
}
import Foundation
import Alamofire
import UIKit
typealias ResponseType = Decodable
typealias AFResponse<Response: Decodable> = Alamofire.DataResponse<ApiResponse<Response>, AFError>
struct AFNetwork: NetworkService {
static func cancelAllRequests() {
@lalkrishna
lalkrishna / HTMLBuilder.swift
Last active March 11, 2022 06:37
Build AttributedString from HTML String content. Fonts can be linked locally.
//
// HTMLBuilder.swift
// HTMLBuilder
//
// Created by Lal Krishna on 11/03/22.
// Copyright © 2022 Lal Krishna. All rights reserved.
//
import Foundation
import UIKit