DMKit 第一篇:DMDefine.h 開(kāi)發(fā)iOS的時(shí)候經(jīng)常用到的宏定義
NSLog
#ifndef __OPTIMIZE__
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...)
#endif
#ifdef DEBUG
#define MyLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define MyLog(...)
#endif
Screen/Frame 屏幕尺寸
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kLineH (1.0f / [UIScreen mainScreen].scale)
//按比例設(shè)置view大小/字體大小等 這里要配合UserInfo單利使用
#define kScale(value) ((value) * [UserInfo shareUser].screenScale)
#define kScaleW(value) ((value) * [UserInfo shareUser].screenScaleW)
#define kGetX(v) (v).frame.origin.x
#define kGetY(v) (v).frame.origin.y
#define kGetW(v) (v).frame.size.width
#define kGetH(v) (v).frame.size.height
#define kGetMinX(v) CGRectGetMinX((v).frame) // 獲得控件屏幕的x坐標(biāo)
#define kGetMinY(v) CGRectGetMinY((v).frame) // 獲得控件屏幕的Y坐標(biāo)
#define kGetMidX(v) CGRectGetMidX((v).frame) //橫坐標(biāo)加上到控件中點(diǎn)坐標(biāo)
#define kGetMidY(v) CGRectGetMidY((v).frame) //縱坐標(biāo)加上到控件中點(diǎn)坐標(biāo)
#define kGetMaxX(v) CGRectGetMaxX((v).frame) //橫坐標(biāo)加上控件的寬度
#define kGetMaxY(v) CGRectGetMaxY((v).frame) //縱坐標(biāo)加上控件的高度
#define kGetTextSize(text, font) [text length] > 0 ? [text sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero //獲取字體size
判斷當(dāng)前的iPhone設(shè)備/系統(tǒng)版本
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
//獲取系統(tǒng)版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//判斷 iOS 8 或更高的系統(tǒng)版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
//獲得app build號(hào)
#define kAppBuild [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]
//獲得app Version 號(hào)
#define kAppVerison [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
Color
//快速獲取顏色
#define kGetColorRGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define kGetColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define kGetColorRgbValue(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]
// 隨機(jī)色
#define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
setValue:賦值的時(shí)候使用
#define DM_STRING(object) (((object == nil) || ([object isEqual:[NSNull null]])) ? @"":[NSString stringWithFormat:@"%@", object])
#define DM_INTEGER(object) ( ((object == nil) || ([object isEqual:[NSNull null]])) ? 0 : [object integerValue])
#define DM_FLOAT(object) ( ((object == nil) || ([object isEqual:[NSNull null]])) ? 0.0f : [object floatValue])
#define DM_DOUBLE(object) ( ((object == nil) || ([object isEqual:[NSNull null]])) ? 0.0f : [object doubleValue])
#define DM_BOOL(object) ( ((object == nil) || ([object isEqual:[NSNull null]])) ? NO : [object boolValue])
#define DM_ARRAY(object) ( ((object == nil) || ([object isEqual:[NSNull null]])) ? @[] : object )
#define DM_DICTIONARY(object) ( ((object == nil) || ([object isEqual:[NSNull null]])) ? @{} : object )
Image
// 加載本地圖片,不緩存
#define kLoadImage(fileName,extName) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:fileName ofType:extName]]
// 加載本地圖片,緩存
#define kGetImage(fileName) [UIImage imageNamed: fileName]
// 加載xib
#define kLoadView(className) [[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil][0]
G-C-D
//后臺(tái)線程運(yùn)行
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
//主線程運(yùn)行
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
//selfWeak
#define WeakObj(self) __weak typeof(self) self##Weak = self;
國(guó)際化
//獲取字符串
#define kLocStr(str) NSLocalizedString(str, nil)
#define kLocStrTab(str,file) NSLocalizedStringFromTable(str, file, nil)
//獲取當(dāng)前語(yǔ)言
#define kCurrentLanguage [[NSLocale preferredLanguages] firstObject]
自己用的大概就這么多吧,能應(yīng)付一般的項(xiàng)目,把它放到pch里面,能使項(xiàng)目少寫(xiě)很多代碼