Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Useful Macros for iOS
//
// TWRMacros.h
// Dispatching
//
// Created by Michelangelo Chasseur on 24/05/14.
// Copyright (c) 2014 Touchware. All rights reserved.
//
#ifndef Dispatching_TWRMacros_h
#define Dispatching_TWRMacros_h
#define TWRApplicationDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
#define TWRUserDefaults [NSUserDefaults standardUserDefaults]
#define TWRNotificationCenter [NSNotificationCenter defaultCenter]
#define TWRSharedApplication [UIApplication sharedApplication]
#define TWRBundle [NSBundle mainBundle]
#define TWRMainScreen [UIScreen mainScreen]
// Network Activity
#define TWRShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define TWRHideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
#define TWRNetworkActivityIndicatorVisible(x) [UIApplication sharedApplication].networkActivityIndicatorVisible = x
#define TWRNavBar self.navigationController.navigationBar
#define TWRTabBar self.tabBarController.tabBar
#define TWRNavBarHeight self.navigationController.navigationBar.bounds.size.height
#define TWRTabBarHeight self.tabBarController.tabBar.bounds.size.height
#define TWRScreenWidth [[UIScreen mainScreen] bounds].size.width
#define TWRScreenHeight [[UIScreen mainScreen] bounds].size.height
// Views and rects
#define TWRViewWidth(v) v.frame.size.width
#define TWRViewHeight(v) v.frame.size.height
#define TWRViewX(v) v.frame.origin.x
#define TWRViewY(v) v.frame.origin.y
#define TWRSelfViewWidth self.view.bounds.size.width
#define TWRSelfViewHeight self.view.bounds.size.height
#define TWRRectX(f) f.origin.x
#define TWRRectY(f) f.origin.y
#define TWRRectWidth(f) f.size.width
#define TWRRectHeight(f) f.size.height
#define TWRRectSetWidth(f, w) CGRectMake(RectX(f), RectY(f), w, RectHeight(f))
#define TWRRectSetHeight(f, h) CGRectMake(RectX(f), RectY(f), RectWidth(f), h)
#define TWRRectSetX(f, x) CGRectMake(x, RectY(f), RectWidth(f), RectHeight(f))
#define TWRRectSetY(f, y) CGRectMake(RectX(f), y, RectWidth(f), RectHeight(f))
#define TWRRectSetSize(f, w, h) CGRectMake(RectX(f), RectY(f), w, h)
#define TWRRectSetOrigin(f, x, y) CGRectMake(x, y, RectWidth(f), RectHeight(f))
// Date
#define TWR_DATE_COMPONENTS NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
#define TWR_TIME_COMPONENTS NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
// Colors
#define TWR_RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#define TWR_RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define TWR_HEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:1.0];
// Devices
#define TWR_isIPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define TWR_isIPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define TWR_isRetinaDevice ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] >= 2)
#define TWR_isMultiTaskingSupported ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] && [[UIDevice currentDevice] isMultitaskingSupported])
// Threading
#define TWR_runOnMainThread if (![NSThread isMainThread]) { dispatch_sync(dispatch_get_main_queue(), ^{ [self performSelector:_cmd]; }); return; };
// Debug
#ifdef DEBUG
#define TWRLog( s, ... ) NSLog( @"<%@:%d> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define TWRLog( s, ... )
#endif
#define TWRLog_Bounds(view) TWRLog(@"%@ bounds: %@", view, NSStringFromRect([view bounds]))
#define TWRLog_Frame(view) TWRLog(@"%@ frame: %@", view, NSStringFromRect([view frame]))
#define TWR_SHOW_VIEW_BORDERS YES
#define TWR_showDebugBorderForViewColor(view, color) if (UA_SHOW_VIEW_BORDERS) { view.layer.borderColor = color.CGColor; view.layer.borderWidth = 1.0; }
#define TWR_showDebugBorderForView(view) UA_showDebugBorderForViewColor(view, [UIColor colorWithRed:1.000 green:0.000 blue:0.024 alpha:1.000])
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.