Skip to content

Instantly share code, notes, and snippets.

@hisoka0917
hisoka0917 / UIImage+SolidColor.swift
Last active May 8, 2018 09:31
Get a UIImage with solid color
extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height:1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 1.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else {
@hisoka0917
hisoka0917 / NSObject+ExtensionProps.swift
Last active January 26, 2018 03:29
Extension property in Swift
extension NSObject {
private struct AssociatedKeys {
static var extensionProps: UInt8 = 0
}
private var extensionProps: NSObject? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.extensionProps) as? NSObject
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.extensionProps, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
@hisoka0917
hisoka0917 / UIColor+hex.swift
Created January 24, 2018 08:54
UIColor with hex in Swift 4
//
// UIColor+hex.swift
//
// Created by Hisoka0917 on 2018/1/24.
// Copyright © 2018. All rights reserved.
//
import UIKit
import Foundation
@hisoka0917
hisoka0917 / JSONCodingKeys.swift
Created January 17, 2018 09:13
Make Array<Any> and Dictionary<String, Any> Decodable
import Foundation
// Copy from https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24
struct JSONCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
@hisoka0917
hisoka0917 / DynamicKeys.swift
Last active September 23, 2021 07:33
Make Dictionary<String, Any> Codable
/**
```
// Encode a model with properties of type [String : Any]
var propertiesContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: .properties)
if let properties = properties {
try propertiesContainer.encodeDynamicKeyValues(withDictionary: properties)
}
```
inspired by https://gist.github.com/samwize/a82f29a1fb34091cd61fc06934568f82
*/
@hisoka0917
hisoka0917 / UIView+frame.swift
Created January 15, 2018 09:48
UIView frame extension
import UIKit
extension UIView {
public var top: CGFloat {
get {
return self.frame.origin.y
}
set {
var frame = self.frame
frame.origin.y = newValue