Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
maxcampolo / UIResponder+FirstResponder.swift
Created August 21, 2017 14:08
UIResponder that finds the current first responder of an iOS application.
//
// UIResponder+FirstResponder.swift
// Thor
//
// Created by Max Campolo on 8/20/17.
// Copyright © 2017 Max Campolo. All rights reserved.
//
import Foundation
import UIKit
@maxcampolo
maxcampolo / FittedLabel.swift
Created November 14, 2016 00:57
Fit UILabel to font size
let label = UILabel()
label.bounds = CGRect(x: 0, y: 0, width: CGFloat(size), height: CGFloat(size))
label.font = UIFont(name: "AppleColorEmoji", size: CGFloat(size))
label.numberOfLines = 0
label.minimumScaleFactor = 0.01
label.adjustsFontSizeToFitWidth = true
label.lineBreakMode = .byTruncatingTail
@maxcampolo
maxcampolo / UIWindow+VisibleViewController.swift
Created September 2, 2016 15:45
Finds the visible view controller in a window heirarchy
public extension UIWindow {
public var visibleViewController: UIViewController? {
return UIWindow.getVisibleViewControllerFrom(self.rootViewController)
}
public static func getVisibleViewControllerFrom(vc: UIViewController?) -> UIViewController? {
if let nc = vc as? UINavigationController {
return UIWindow.getVisibleViewControllerFrom(nc.visibleViewController)
} else if let tc = vc as? UITabBarController {
return UIWindow.getVisibleViewControllerFrom(tc.selectedViewController)
@maxcampolo
maxcampolo / Double+Suffix.swift
Created August 30, 2016 16:28
Format a double into a short string with units suffix
extension Double {
var suffixNumber: String {
get {
var num = self
let sign = num < 0 ? "-" : ""
num = fabs(num)
if num < 1000.0 {
return "\(sign)\(Int(num))"
@maxcampolo
maxcampolo / UILabel+WillTruncate.swift
Created August 29, 2016 18:51
Extension that calculates if a UILabel will be truncated depending on its number of lines
extension UILabel {
func willTruncate() -> Bool {
guard let labelText = text else {
return false
}
let mText = labelText as NSString
let attributes = [NSFontAttributeName : font]
let labelSize = mText.boundingRectWithSize(CGSize(width: bounds.width, height: CGFloat.max), options: .UsesLineFragmentOrigin, attributes: attributes, context: nil)
return Int(ceil(CGFloat(labelSize.height) / font.lineHeight)) > numberOfLines
@maxcampolo
maxcampolo / UIView+Shadow.swift
Created August 29, 2016 16:39
Extension to add a shadow to a UIView
extension UIView {
/**
Set a shadow on a UIView.
- parameters:
- color: Shadow color, defaults to black
- opacity: Shadow opacity, defaults to 1.0
- offset: Shadow offset, defaults to zero
- radius: Shadow radius, defaults to 0
- viewCornerRadius: If the UIView has a corner radius this must be set to match
@maxcampolo
maxcampolo / GitCommands.script
Last active August 11, 2016 13:43
Handy Git cheat sheet.
// Remove or add a file
git rm path/to/file
git add path/to/file
// Resetting state
// Revert working copy
git checkout .
// Revert unpushed commits
git reset
// Revert one commit
@maxcampolo
maxcampolo / GlobalConstant.swift
Created August 10, 2016 14:58
Global constants with swift extensions and Implicit Member Expression.
extension Double {
public static let kRectX = 30.0
public static let kRectY = 30.0
public static let kRectWidth = 30.0
public static let kRectHeight = 30.0
}
public func makeRect() -> CGRect {
return CGRect(x: .kRectX, y: .kRectY, width: .kRectWidth, height: .kRectHeight)
}
@maxcampolo
maxcampolo / FileCount.sh
Created August 9, 2016 19:51
Count number of files with a specified extension.
// Recursive
find . -name "*.filetype" | wc -l
// Nonrecursive
ls -l *.filetype | wc -l
@maxcampolo
maxcampolo / AutoIncrement.script
Last active August 8, 2016 17:30
Auto-increment target build number in Xcode.
#!/bin/bash
# Auto Increment Version Script
buildPlist=$INFOPLIST_FILE
echo $buildPlist
CFSVString=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$buildPlist")
CFBundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$buildPlist")
BUILD_NR=${CFBundleVersion##*.}
BUILD_NR=$(($BUILD_NR + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NR" "$buildPlist"