Skip to content

Instantly share code, notes, and snippets.

@rudyryk
Created April 27, 2014 15:41
Show Gist options
  • Save rudyryk/11348772 to your computer and use it in GitHub Desktop.
Save rudyryk/11348772 to your computer and use it in GitHub Desktop.
Objective-C and Cocoa shortcuts for iOS development
//
// HyperShortcuts.h
// Created by Alexey Kinyov on 27/04/2014.
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Alexey Kinyov <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#ifndef HyperShortcuts_h
#define HyperShortcuts_h
/* CGRect */
#define CGRectSetX(rect, x) CGRectMake(x, rect.origin.y, rect.size.width, rect.size.height)
#define CGRectSetY(rect, y) CGRectMake(rect.origin.x, y, rect.size.width, rect.size.height)
#define CGRectSetPos(rect, x, y) CGRectMake(x, y, rect.size.width, rect.size.height)
#define CGRectSetHeight(rect, h) CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, h)
#define CGRectSetWidth(rect, w) CGRectMake(rect.origin.x, rect.origin.y, w, rect.size.height)
#define CGRectSetSize(rect, size) CGRectMake(rect.origin.x, rect.origin.y, size.width, size.height)
#define CGRectSetScale(rect, scale) CGRectMake(rect.origin.x, rect.origin.y, scale*rect.size.width, scale*rect.size.height)
/* Application */
#define app_ui() [UIApplication sharedApplication]
#define app_docs_dir() [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]
/* Localization */
#define _(str) NSLocalizedString(str, nil)
/* Math */
#define M_FI_SML (sqrt(5.0) - 1.0) / 2 // Golden ratio
#define M_FI_BIG (sqrt(5.0) + 1.0) / 2 // Golden ratio
/* Strings */
#define str_fix_null(s) (([[NSNull null] isEqual:s] || s == nil) ? @"" : s)
#define str_equal(x, y) [x isEqualToString:y]
#define str_format(fmt, ...) ([NSString stringWithFormat:fmt, __VA_ARGS__])
#define str_replace(s, occur, replace) [s stringByReplacingOccurrencesOfString:occur withString:replace]
#define str_contains(s, sub) ([s rangeOfString:sub].location != NSNotFound)
#define str_to_long(s) [(NSString *)s longLongValue]
#define str_to_num(s) [NSNumber numberWithLongLong:[(NSString *)s longLongValue]]
/* JSON */
#define JSON_parse(json) [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:nil]
#define JSON_string(dict) (\
[[NSString alloc] initWithData:\
[NSJSONSerialization dataWithJSONObject:dict\
options:NSJSONWritingPrettyPrinted\
error:nil]\
encoding:NSUTF8StringEncoding])
/* Network */
#define url(s) [NSURL URLWithString:s]
/* Date and time */
#define timestamp() (NSUInteger)round([[NSDate date] timeIntervalSince1970])
#define timeago(t) (NSInteger)round(timestamp() - t)
/* Dictionary */
#define dict_get(d, k) ([[d class] isSubclassOfClass:[NSDictionary class]] ? [(NSDictionary *)d objectForKey:k] : nil)
#define dict_set(d, k, v) [(NSMutableDictionary *)d setObject:v forKey:k]
/* Numbers */
#define num_by_int(n) [[NSNumber alloc] initWithInteger:n]
#define num_equal(x, y) [x isEqualToNumber:y]
/* UI */
#define ui_image(name) [UIImage imageNamed:name]
#define ui_image_view(name) [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]]
#define ui_font(_name, _size) [UIFont fontWithName:_name size:_size]
/* UI - Colors */
#define color(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define color_by_image(name) [UIColor colorWithPatternImage:ui_image(name)]
#define color_alpha(c, a) [c colorWithAlphaComponent:a]
#define color_white(a) color(255, 255, 255, a)
/* UI - Gestures */
#define ui_handle_tap(view, delegate, selector) do {\
view.userInteractionEnabled = YES;\
[view addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:delegate action:selector]];\
} while(0)
#define ui_handle_lswipe(view, delegate, selector) do {\
view.userInteractionEnabled = YES;\
UISwipeGestureRecognizer *rec = [[UISwipeGestureRecognizer alloc] initWithTarget:delegate action:selector];\
[rec setDirection:(UISwipeGestureRecognizerDirectionLeft)];\
[view addGestureRecognizer: rec];\
} while(0)
#define ui_handle_rswipe(view, delegate, selector) do {\
view.userInteractionEnabled = YES;\
UISwipeGestureRecognizer *rec = [[UISwipeGestureRecognizer alloc] initWithTarget:delegate action:selector];\
[rec setDirection:(UISwipeGestureRecognizerDirectionRight)];\
[view addGestureRecognizer: rec];\
} while(0)
#define ui_handle_downswipe(view, delegate, selector) do {\
view.userInteractionEnabled = YES;\
UISwipeGestureRecognizer *rec = [[UISwipeGestureRecognizer alloc] initWithTarget:delegate action:selector];\
[rec setDirection:(UISwipeGestureRecognizerDirectionDown)];\
[view addGestureRecognizer: rec];\
} while(0)
#define ui_clear_gestures(view) do {\
while (view.gestureRecognizers.count) {\
[view removeGestureRecognizer:view.gestureRecognizers.lastObject];\
}\
} while(0)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment