Created
September 23, 2013 08:55
-
-
Save zengyun-hacker/6668088 to your computer and use it in GitHub Desktop.
common macro in iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - Log | |
//debug log | |
#ifdef DEBUG | |
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); | |
# define DLogRect(rect) DLog(@"%s x=%f, y=%f, w=%f, h=%f", #rect, rect.origin.x, rect.origin.y,rect.size.width, rect.size.height) | |
# define DLogPoint(pt) DLog(@"%s x=%f, y=%f", #pt, pt.x, pt.y) | |
# define DLogSize(size) DLog(@"%s w=%f, h=%f", #size, size.width, size.height) | |
# define DLogColor(_COLOR) DLog(@"%s h=%f, s=%f, v=%f", #_COLOR, _COLOR.hue, _COLOR.saturation, _COLOR.value) | |
# define DLogSuperViews(_VIEW) { for (UIView* view = _VIEW; view; view = view.superview) { GBLog(@"%@", view); } } | |
# define DLogSubViews(_VIEW) \ | |
{ for (UIView* view in [_VIEW subviews]) { GBLog(@"%@", view); } } | |
# else | |
# define DLog(...) | |
# define DLogRect(rect) | |
# define DLogPoint(pt) | |
# define DLogSize(size) | |
# define DLogColor(_COLOR) | |
# define DLogSuperViews(_VIEW) | |
# define DLogSubViews(_VIEW) | |
# endif | |
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); | |
//log with UIAlertView | |
#ifdef DEBUG | |
# define ULog(fmt, ...) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } | |
#else | |
# define ULog(...) | |
#endif | |
//mark | |
#define MARK NSLog(@"\nMARK: %s, %d", __PRETTY_FUNCTION__, __LINE__) | |
#pragma mark - Device Information | |
#define isiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO) | |
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO) | |
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) | |
#define isSimulator (NSNotFound != [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"].location) | |
#define NavigationBar_HEIGHT 44 | |
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) | |
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) | |
#pragma mark - System Information | |
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue] | |
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion]) | |
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0]) | |
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) | |
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) | |
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) | |
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) | |
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) | |
#pragma mark - Some Functions | |
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]; | |
#define NOTIFICATION_CENTER [NSNotificationCenter defaultCenter] | |
#define IMAGE_CACHE [SDImageCache sharedImageCache] | |
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1] | |
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)] | |
#define LOCALSTRING(x, ...) NSLocalizedString(x, nil) | |
// rgb converter(hex->dec) | |
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] | |
#define FONT(x) [UIFont systemFontOfSize:x] | |
//读取本地图片 | |
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]] | |
//documents structure of application | |
#define APP_DOCUMENT [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] | |
#define APP_LIBRARY [NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] | |
#define APP_CACHES_PATH [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] | |
#define APP_USERINFO_PATH userInfoPath() | |
#pragma mark - Snippet | |
#define WEAKSELF typeof(self) __weak weakSelf = self; | |
#define STRONGSELF typeof(weakSelf) __strong strongSelf = weakSelf; | |
// show Alert | |
#define alertContent(content) \ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" \ | |
message:content \ | |
delegate:nil \ | |
cancelButtonTitle:@"确定" \ | |
otherButtonTitles:nil]; \ | |
[alert show]; \ | |
//singleton | |
// | |
// SynthesizeSingleton.h | |
// CocoaWithLove | |
// | |
// Created by Matt Gallagher on 20/10/08. | |
// Copyright 2009 Matt Gallagher. All rights reserved. | |
// | |
// Permission is given to use this source code file without charge in any | |
// project, commercial or otherwise, entirely at your risk, with the condition | |
// that any redistribution (in part or whole) of source code must retain | |
// this copyright and permission notice. Attribution in compiled projects is | |
// appreciated but not required. | |
// | |
#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \ | |
\ | |
static classname *shared##classname = nil; \ | |
\ | |
+ (classname *)shared##classname \ | |
{ \ | |
@synchronized(self) \ | |
{ \ | |
if (shared##classname == nil) \ | |
{ \ | |
shared##classname = [[self alloc] init]; \ | |
} \ | |
} \ | |
\ | |
return shared##classname; \ | |
} \ | |
\ | |
+ (id)allocWithZone:(NSZone *)zone \ | |
{ \ | |
@synchronized(self) \ | |
{ \ | |
if (shared##classname == nil) \ | |
{ \ | |
shared##classname = [super allocWithZone:zone]; \ | |
return shared##classname; \ | |
} \ | |
} \ | |
\ | |
return nil; \ | |
} \ | |
\ | |
- (id)copyWithZone:(NSZone *)zone \ | |
{ \ | |
return self; \ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
define WEAKSELF typeof(self) __weak weakSelf = self;
define STRONGSELF typeof(weakSelf) __strong strongSelf = weakSelf;
they are not work