Created
April 21, 2017 16:37
-
-
Save jarvisluong/21d01ad766a5cc50def1de320c2a7c74 to your computer and use it in GitHub Desktop.
Random utilities in UIKit Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
extension UIColor { | |
convenience init(colorArray array: [CGFloat]) { | |
let r = array[0] | |
let g = array[1] | |
let b = array[2] | |
self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha:1.0) | |
} | |
} | |
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, 0.0) | |
color.setFill() | |
UIRectFill(rect) | |
let image = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
guard let cgImage = image?.cgImage else { return nil } | |
self.init(cgImage: cgImage) | |
} | |
func filledImage(fillColor: UIColor) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale) | |
let context = UIGraphicsGetCurrentContext()! | |
fillColor.setFill() | |
context.translateBy(x: 0, y: self.size.height) | |
context.scaleBy(x: 1.0, y: -1.0) | |
context.setBlendMode(CGBlendMode.colorBurn) | |
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) | |
context.draw(self.cgImage!, in: rect) | |
context.setBlendMode(CGBlendMode.sourceIn) | |
context.addRect(rect) | |
context.drawPath(using: CGPathDrawingMode.fill) | |
let coloredImg : UIImage = UIGraphicsGetImageFromCurrentImageContext()! | |
UIGraphicsEndImageContext() | |
return coloredImg | |
} | |
} | |
extension UITextField { | |
func useUnderline(_ color: UIColor) { | |
let border = CALayer() | |
let borderWidth = CGFloat(1.0) | |
border.borderColor = color.cgColor | |
border.frame = CGRect(x: 0, y: self.frame.size.height - borderWidth, width: self.frame.size.width, height: self.frame.size.height) | |
border.borderWidth = borderWidth | |
self.layer.addSublayer(border) | |
self.layer.masksToBounds = true | |
} | |
} | |
extension UIView { | |
func applyGradient(colours: [UIColor]) -> Void { | |
self.applyGradient(colours: colours, locations: nil) | |
} | |
func applyGradient(colours: [UIColor], locations: [NSNumber]?) -> Void { | |
let gradient: CAGradientLayer = CAGradientLayer() | |
gradient.frame = self.bounds | |
gradient.colors = colours.map { $0.cgColor } | |
gradient.locations = locations | |
self.layer.insertSublayer(gradient, at: 0) | |
} | |
} | |
extension String { | |
static func isBlank(_ optionalString :String?) -> Bool { | |
if let string = optionalString { | |
return string.isEmpty | |
} else { | |
return true | |
} | |
} | |
func convertToJSONObject() -> [String: Any]? { | |
if let data = self.data(using: .utf8) { | |
do { | |
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] | |
} catch { | |
print(error.localizedDescription) | |
} | |
} | |
return nil | |
} | |
} | |
public extension Sequence { | |
func categorise<U : Hashable>(_ key: (Iterator.Element) -> U) -> [U:[Iterator.Element]] { | |
var dict: [U:[Iterator.Element]] = [:] | |
for el in self { | |
let key = key(el) | |
if case nil = dict[key]?.append(el) { dict[key] = [el] } | |
} | |
return dict | |
} | |
} | |
extension UIButton { | |
func setBackgroundColor(_ color: UIColor, for state: UIControlState) { | |
self.setBackgroundImage(UIImage(color: color), for: state) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment