Skip to content

Instantly share code, notes, and snippets.

@mteera
mteera / Resize UIImage
Created March 14, 2018 11:22
A function to let you resize UIImage for example for a the imageView on a UITableViewCell
func resizeImageWith(image: UIImage, newSize: CGSize) -> UIImage {
let horizontalRatio = newSize.width / image.size.width
let verticalRatio = newSize.height / image.size.height
let ratio = max(horizontalRatio, verticalRatio)
let newSize = CGSize(width: image.size.width * ratio, height: image.size.height * ratio)
var newImage: UIImage
if #available(iOS 10.0, *) {
@mteera
mteera / JSONStringify updated for Swift 4
Created January 26, 2018 14:17
A function to print JSON objects for readability.
func JSONStringify(value: AnyObject, prettyPrinted: Bool = true) -> String {
let options = prettyPrinted ? JSONSerialization.WritingOptions.prettyPrinted : nil
if JSONSerialization.isValidJSONObject(value) {
do {
let data = try JSONSerialization.data(withJSONObject: value, options: options!)
if let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
return string as String
}
} catch {
return ""
@mteera
mteera / isSeparatorHidden
Last active January 22, 2018 15:44
An extension for UITableViewCell's to hide the seperator
extension UITableViewCell {
var isSeparatorHidden: Bool {
get {
return self.separatorInset.right != 0
}
set {
if newValue {
self.separatorInset = UIEdgeInsetsMake(0, self.bounds.size.width, 0, 0)
} else {
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
@mteera
mteera / UIView Rounded Corners
Last active January 5, 2018 09:44
An extension to create rounded corners on particular edges.
import UIKit
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
@mteera
mteera / Custom UIColor+Extension
Last active December 4, 2017 11:32
An extension to create custom colours and use hex values.
import UIKit
// creating a color object
extension UIColor {
static let foodBulbGreen = UIColor(r: 47, g: 212, b: 128, a: 1)
static let foodBulbOrange = UIColor(r: 255, g: 130, b: 98, a: 1)
}
// usage: UIColor.foodBulbGreen
@mteera
mteera / Custom UITextField
Last active January 13, 2020 02:45
Custom UITextField using @IBDesignable with Swift 3
import UIKit
@IBDesignable class CustomUITextField: UITextField {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}