Skip to content

Instantly share code, notes, and snippets.

View jrturton's full-sized avatar

Richard Turton jrturton

View GitHub Profile
import UIKit
// Snapshot utilities
extension UIView {
func snapshotView(view: UIView, afterUpdates: Bool) -> UIView {
let snapshot = view.snapshotViewAfterScreenUpdates(afterUpdates)
self.addSubview(snapshot)
snapshot.frame = convertRect(view.bounds, fromView: view)
return snapshot
@jrturton
jrturton / testimage.swift
Created November 19, 2018 14:59
Make a date-stamped test image
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 400, height: 400))
let image = renderer.image {
context in
UIColor.lightGray.setFill()
UIRectFill(context.format.bounds)
UIColor.black.set()
UIRectFrame(context.format.bounds)
let text = "TEST IMAGE \n\(Date())" as NSString
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
@jrturton
jrturton / UIButton+Insets.m
Created June 27, 2013 13:18
Handy inset adjustments for UIButtons
@implementation UIButton (Insets)
-(void)addSpaceBetweenImageAndText:(CGFloat)space
{
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, space, 0.0, -space);
self.contentEdgeInsets = UIEdgeInsetsMake(0.0, space, 0.0, 2.0 * space);
}
- (void)addSpaceBetweenImageAndText:(CGFloat)space withLeftAndRightPadding:(CGFloat)padding
{
@jrturton
jrturton / gist:89ce72a87e1b0661eccb
Created October 2, 2014 13:30
Removing a core data store
-(void)removeDataStoreAtURL:(NSURL*)url
{
// Need to remove anything matching the first part - there are also logging files alongside the SQLite file itself.
// Typical directory contents are:
// Name.sqlite, Name.sqlite-shm, Name.sqlite-wal
NSURL *folderURL = [url URLByDeletingLastPathComponent];
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:nil options:0 error:nil];
@jrturton
jrturton / Get keyboard frame in local coordinates after receiving notification
Last active September 28, 2016 12:36
Getting the keyboard's frame in local coordinates
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view.window convertRect:keyboardFrame fromWindow:nil];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
@jrturton
jrturton / gist:6881601
Created October 8, 2013 08:41
Increment build number at every archive or release build
if [ $CONFIGURATION = "Release" ] || [ $CONFIGURATION = "Dev" ]; then
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
fi
@jrturton
jrturton / gist:6364558
Last active December 21, 2015 20:58
Navigation back buttons themed like iOS7, for iOS 5 and 6
-(void)themeBackButtons
{
// Appearance proxy methods
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage backButtonImage] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(7.0, 0.0) forBarMetrics:UIBarMetricsDefault];
}
// The image above is generated by this method
+(UIImage *)backButtonImage
{
@jrturton
jrturton / UIImage+HLImages.m
Created July 31, 2013 10:49
Tint mono image
-(UIImage*)tintedMonoImageWithColor:(UIColor*)color
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
[color setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0];
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
@jrturton
jrturton / NSString+URLCharacters.m
Created July 31, 2013 09:53
Escaping reserved URL characters
-(NSString*)stringByPercentEscapingReservedURLCharacters
{
NSString *cleanedString = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)self, NULL, (CFStringRef)@"!#$&'()*+,/:;=?@[]", kCFStringEncodingUTF8));
return cleanedString;
}
#import "UIResponder+FirstResponder.h"
static __weak id currentFirstResponder;
@implementation UIResponder (FirstResponder)
+(id)currentFirstResponder {
currentFirstResponder = nil;
[[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil];
return currentFirstResponder;