Skip to content

Instantly share code, notes, and snippets.

View migueltg's full-sized avatar

Miguel migueltg

  • Madrid, España
View GitHub Profile
@migueltg
migueltg / ColorWithHexString.swift
Last active April 13, 2020 12:29
Swift function that returns a UIColor from a color in hexadecimal
func colorWithHexString (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
let index = cString.index(cString.startIndex, offsetBy: 1)
cString = cString.substring(from: index)
}
if (cString.characters.count != 6) {
return UIColor.gray
}
@migueltg
migueltg / SetTitleAndSubtitleInNavigationBar.swift
Last active November 15, 2017 09:20
Swift function for UINavigationItem extension to set title and subtitle in iOS navigation bar
extension UINavigationItem {
//Function to set title and subtitle in navigation bar
func setTitle(title:String, subtitle:String) {
let titleLabel = UILabel()
titleLabel.text = title
titleLabel.font = UIFont(name: "Roboto-Regular", size: 17)
titleLabel.textColor = UIColor.white
titleLabel.sizeToFit()
extension UIDevice {
static var isSimulator: Bool {
return ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil
}
}
@migueltg
migueltg / ColorInitWithHexString.swift
Created June 7, 2017 12:55
UIColor init extension with hexadecimal
extension UIColor {
convenience init?(hex:String){
var cString:String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
let index = cString.index(cString.startIndex, offsetBy: 1)
cString = cString.substring(from: index)
}
if (cString.characters.count != 6) {
return nil
@migueltg
migueltg / Localized.swift
Last active July 10, 2017 13:13
Function for get locales from .strings in Xcode
func localized(_ string: String) -> String {
return NSLocalizedString(string, comment: "")
}
@migueltg
migueltg / RandomString.swift
Created July 24, 2017 13:20
Generate random string with custom length
extension String {
static func random(_ length: Int = 20) -> String {
let base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var randomString: String = ""
for _ in 0..<length {
let randomValue = arc4random_uniform(UInt32(base.characters.count))
randomString += "\(base[base.characters.index(base.startIndex, offsetBy: Int(randomValue))])"
}
@migueltg
migueltg / KeyboardHandling.swift
Last active July 24, 2017 13:26
Keyboard Handling in UIViewController
import UIKit
private var viewScrollViewAssociatedKey: UInt8 = 0
extension UIViewController {
var scrollViewInternal: UIScrollView! {
get {
return objc_getAssociatedObject(self, &viewScrollViewAssociatedKey) as? UIScrollView
}
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
}
@migueltg
migueltg / pre-commit-eslint
Last active April 27, 2018 10:50 — forked from shettayyy/pre-commit-eslint
Pre-commit hook for Linting JS with ESLint before commit.
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
@migueltg
migueltg / loadViewFromNib.swift
Created May 31, 2018 15:10
Load view from nib in xcode for storyboard and code
func loadViewFromNib(nibName: String? = "\(self)") {
guard let nibName = nibName else { return }
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
guard let view = nib.instantiate(withOwner: self, options: nil).first as? UIView else { return }
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
}