Skip to content

Instantly share code, notes, and snippets.

View karambirov's full-sized avatar
📱
iOS developer

Eugene Karambirov karambirov

📱
iOS developer
View GitHub Profile
extension String {
public func fuzzilyMatches(string: String, separatorsSet: CharacterSet = .whitespacesAndNewlines) -> Bool {
let matchComponents = string.lowercased().components(separatedBy: separatorsSet)
return lowercased()
.components(separatedBy: separatorsSet)
.contains { candidate in
matchComponents.contains {
candidate.contains($0)
import Foundation
public extension UIView {
func compactSystemLayoutFittingSizeFor(width: CGFloat) -> CGSize {
let targetSize = CGSize(
width: width,
height: UIView.layoutFittingCompressedSize.height
)
@karambirov
karambirov / Array+withoutDuplicates.swift
Created February 2, 2023 07:31
Swift array extension for removing duplicates
public extension Array where Element: Hashable {
func withoutDuplicates() -> [Element] {
var seen = Set<Element>()
return filter { seen.insert($0).inserted }
}
}
@karambirov
karambirov / String+calculateSize.swift
Created January 30, 2023 10:32
Extensions for calculation size of a Swift String
import Foundation
public extension String {
func calculateSize(attributes: [NSAttributedString.Key: Any], constrainedSize: CGSize) -> CGSize {
let rect = (self as NSString).boundingRect(
with: constrainedSize,
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: attributes,
context: nil
@karambirov
karambirov / IgnoreHashable.swift
Created October 17, 2022 08:57
Allows to ignore properties that prevent auto-conforming to Hashable
private let hashableId = UUID()
@propertyWrapper
public struct IgnoreHashable<T>: Hashable {
public let wrappedValue: T
public init(wrappedValue: T) {
if wrappedValue is AnyHashable {
AssertionFailure("Hashable types shouldn't ignore its hash value")
@karambirov
karambirov / HostingCollectionReusableView.swift
Created August 23, 2022 07:51
Wrappers for UICollectionReusableView and UICollectionViewCell which allow to use SwiftUI views in UIKit UICollectionView
import SwiftUI
public class HostingCollectionReusableView<Content: View>: UICollectionReusableView {
private let hostingController = UIHostingController<Content?>(rootView: nil, ignoreSafeArea: true)
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
@karambirov
karambirov / UIControl+onTouchUpInside.swift
Created June 15, 2022 05:18
Allows to set a Swift closure to a control instead of Objective-C selector
import UIKit
public extension UIControl {
typealias OnTouchUpInside = () -> Void
private struct Constants {
static var callbackKey = "UIControl.OnTouchUpInsideAssociatedKey"
}
@karambirov
karambirov / UIHostingController+SafeArea.swift
Last active May 12, 2024 08:12
How to ignore safe area in UIHostingController
import SwiftUI
extension UIHostingController {
convenience public init(rootView: Content, ignoreSafeArea: Bool) {
self.init(rootView: rootView)
if ignoreSafeArea {
disableSafeArea()
}