Skip to content

Instantly share code, notes, and snippets.

import UIKit
class DemoViewController: UIViewController {
@IBOutlet weak var highlightedLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Subscribe this instance of UIViewController to theme notifications
ThemeManager.shared.register(
public protocol ThemeModelProtocol {}
public protocol ThemeProtocol: Equatable {
    associatedtype Model: ThemeModelProtocol
    var settings: Model { get }
}
struct MyThemeSettings: ThemeModelProtocol {
let appBgColor: UIColor
let highlightedBgColor: UIColor
let textColor: UIColor
}
enum MyTheme: ThemeProtocol {
case light
case dark
var settings: MyThemeSettings {
extension MyThemeSettings {
static let lightTheme = MyThemeSettings(
appBgColor: .white,
highlightedBgColor: .lightGray,
textColor: .black
)
static let darkTheme = MyThemeSettings(
appBgColor: .gray,
highlightedBgColor: .darkGray,
textColor: .white
protocol TargetAction {
associatedtype Theme: ThemeProtocol
func applyTheme(_ theme: Theme)
}
struct AnyTargetActionWrapper<Theme: ThemeProtocol>: TargetAction {
private let _applyTheme: (Theme) -> ()
init<Target: AnyObject>(
target: Target,
import Foundation
fileprivate var associatedKey = "ThemerTargetActionStorage"
final class TargetActionStorage<Theme: ThemeProtocol> {
private var targetActions: [AnyTargetActionWrapper<Theme>] = []
static func setup(for target: Any) -> TargetActionStorage {
let storage = TargetActionStorage()
objc_setAssociatedObject(
import Foundation
final class Themer<Theme: ThemeProtocol> {
typealias TargetActionStorageType = TargetActionStorage<Theme>
private var targetActionStorages = NSHashTable<TargetActionStorageType>
.weakObjects()
var theme: Theme {
didSet {
extension Themer where Theme == MyTheme {
    private static var instance: Themer?
    static var shared: Themer {
        if instance == nil {
            instance = Themer(defaultTheme: .light)
        }
        return instance!
    }
}
//
// TitlePosition.swift
// PileCollectionVC
//
// Created by Rik Ki on 11/9/18.
// Copyright © 2018 Rik Ki. All rights reserved.
//
import Foundation
@serhiybutz
serhiybutz / AutosizingTextField.swift
Last active September 21, 2020 00:35
Dynamic width NSTextField
//
// AutosizingTextField.swift
//
// Created by Serge Bouts on 6/4/20.
// Copyright © 2020 Rik Ki. All rights reserved.
//
import Cocoa
final class AutosizingTextField: NSTextField {