美文网首页iOS Developer
常用的宏(可直接拿来用)

常用的宏(可直接拿来用)

作者: 码了个农啵 | 来源:发表于2017-01-05 13:45 被阅读0次

    //----------------------图片相关----------------------------

    //读取本地图片

    #define LoadImage(file,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]

    #define LoadPNGImage(file) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:@"png"]]

    //定义UIImage对象

    #define ImageNamed(name) [UIImage imageNamed:name]

    //可拉伸的图片

    #define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]

    #define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]

    //图片尺寸

    #define CustomSizeImage(url,width,height) [NSString stringWithFormat:@"%@?imageView2/1/w/%d/h/%d",url,width,height]

    //----------------------颜色相关---------------------------

    // rgb颜色转换(16进制->10进制)

    #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]

    // 获取RGB颜色

    #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]

    #define RGB(r,g,b) RGBA(r,g,b,1.0f)

    //字体定义

    #define FONT(F) [UIFont systemFontOfSize:F]

    //[UIFont fontWithName:@"Source Han Sans CN" size:F]

    #define FONTSYSTEMBOLD(F) [UIFont boldSystemFontOfSize:F]

    //----------------------系统设备相关----------------------------

    //获取Window

    #define WINDOW [UIApplication sharedApplication].keyWindow

    //获取设备屏幕尺寸

    #define ScreenWidth ([UIScreen mainScreen].bounds.size.width)

    #define ScreenHeight ([UIScreen mainScreen].bounds.size.height)

    #define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width

    #define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height

    //获取系统版本

    #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

    #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]

    #define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)

    #define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)

    #define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)

    #define isIOS7 ([[[UIDevice currentDevice] systemVersion] intValue]==7)

    #define isIOS8 ([[[UIDevice currentDevice] systemVersion] intValue]==8)

    #define isIOS9 ([[[UIDevice currentDevice] systemVersion] intValue]==9)

    #define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)

    #define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)

    #define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)

    #define isAfterIOS7 ([[[UIDevice currentDevice] systemVersion] intValue]>7)

    #define isAfterIOS8 ([[[UIDevice currentDevice] systemVersion] intValue]>8)

    #define isIOS4OrAfter ([[[UIDevice currentDevice] systemVersion] intValue]>=4)

    #define isIOS5OrAfter ([[[UIDevice currentDevice] systemVersion] intValue]>=5)

    #define isIOS6OrAfter ([[[UIDevice currentDevice] systemVersion] intValue]>=6)

    #define isIOS7OrAfter ([[[UIDevice currentDevice] systemVersion] intValue]>=7)

    #define isIOS8OrAfter ([[[UIDevice currentDevice] systemVersion] intValue]>=8)

    //获取当前语言

    #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

    //判断是否 Retina屏、设备是否iphone 5、是否是iPad

    #define isIphone6OrAfter [[UIScreen mainScreen] bounds].size.width > 320.0f

    #define iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

    #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

    #define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)

    #define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

    #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

    //判断是真机还是模拟器

    #if TARGET_OS_IPHONE

    //iPhone Device

    #endif

    #if TARGET_IPHONE_SIMULATOR

    //iPhone Simulator

    #endif

    //----------------------视图相关----------------------------

    //得到视图的left top的X,Y坐标点

    #define VIEW_TX(view) (view.frame.origin.x)

    #define VIEW_TY(view) (view.frame.origin.y)

    //得到视图的right bottom的X,Y坐标点

    #define VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)

    #define VIEW_BY(view) (view.frame.origin.y + view.frame.size.height)

    //得到视图的尺寸:宽度、高度

    #define VIEW_W(view)  (view.frame.size.width)

    #define VIEW_H(view)  (view.frame.size.height)

    //得到frame的X,Y坐标点

    #define FRAME_X(frame)  (frame.origin.x)

    #define FRAME_Y(frame)  (frame.origin.y)

    //得到frame的宽度、高度

    #define FRAME_W(frame)  (frame.size.width)

    #define FRAME_H(frame)  (frame.size.height)

    //---------------------打印日志--------------------------

    //打印Frame

    #define LogFrame(frame) NSLog(@"Frame[X=%.1f,Y=%.1f,W=%.1f,H=%.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)

    //打印Size

    #define LogSize(size) NSLog(@"Size[W=%.1f,H=%.1f",size.width,size.height)

    //打印Point

    #define LogPoint(point) NSLog(@"Point[X=%.1f,Y=%.1f]",point.x,point.y)

    //打印Log

    #define Log(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);

    //file

    //读取文件的文本内容,默认编码为UTF-8

    #define FileString(name,ext)            [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]

    #define FileDictionary(name,ext)        [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]

    #define FileArray(name,ext)            [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]

    //G-C-D

    #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)

    #define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)

    //Alert

    #define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]

    //----------------------程序相关----------------------------

    // 应用程序托管

    #define AppDelegateInstance ((AppDelegate*)([UIApplication sharedApplication].delegate))

    // 本地存储

    #define UserDefaults [NSUserDefaults standardUserDefaults]

    // 通知中心

    #define NotificationCenter [NSNotificationCenter defaultCenter]

    //由角度获取弧度 有弧度获取角度

    #define degreesToRadian(x) (M_PI * (x) / 180.0)

    #define radianToDegrees(radian) (radian*180.0)/(M_PI)

    //字符串

    #define AppendString(string,addstring) [string stringByAppendingString:addstring]

    //----------------------内存相关----------------------------

    #ifndef weakify

    #if DEBUG

    #if __has_feature(objc_arc)

    #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;

    #else

    #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;

    #endif

    #else

    #if __has_feature(objc_arc)

    #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;

    #else

    #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;

    #endif

    #endif

    #endif

    #ifndef strongify

    #if DEBUG

    #if __has_feature(objc_arc)

    #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;

    #else

    #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;

    #endif

    #else

    #if __has_feature(objc_arc)

    #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;

    #else

    #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;

    #endif

    #endif

    #endif

    // block self

    #define WEAKSELF typeof(self) __weak weakSelf = self;

    #define STRONGSELF typeof(weakSelf) __strong strongSelf = weakSelf;

    //释放一个对象

    #define SAFE_DELETE(P) if(P) { [P release], P = nil; }

    #define SAFE_RELEASE(x) [x release];x=nil

    #define SuppressPerformSelectorLeakWarning(Stuff) \

    do { \

    _Pragma("clang diagnostic push") \

    _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \

    Stuff; \

    _Pragma("clang diagnostic pop") \

    } while (0)

    相关文章

      网友评论

        本文标题:常用的宏(可直接拿来用)

        本文链接:https://www.haomeiwen.com/subject/vmlzvttx.html