系統(tǒng)高度
//屏幕的寬高
#define HitoScreenW [UIScreen mainScreen].bounds.size.width
#define HitoScreenH [UIScreen mainScreen].bounds.size.height
//屏幕大小
#define HitoScreenSize [UIScreen mainScreen].bounds
//比例寬和高(以6s為除數(shù))
#define HitoActureHeight(height) roundf(height/375.0 * HitoScreenW)
#define HitoActureWidth(Width) roundf(Width/667.0 * HitoScreenH)
//狀態(tài)欄的高度
#define HitoStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
//導航欄的高度
#define HitoNavBarHeight 44.0
//iphoneX-SafeArea的高度
#define HitoSafeAreaHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?34:0)
//分欄+iphoneX-SafeArea的高度
#define HitoTabBarHeight (49+HitoSafeAreaHeight)
//導航欄+狀態(tài)欄的高度
#define HitoTopHeight (HitoStatusBarHeight + HitoNavBarHeight)
快速查詢一段代碼的執(zhí)行時間
#define TOCK NSLog(@"Time:%f", -[[NSDate date] timeIntervalSinceNow]);
blcok 和self 引起的循環(huán)引用警告??的處理問題
#define Custom_Weak(weakSelf) __weak __typeof(&*self)weakSelf = self;
代碼中加入NSLog的暴力調試方法是很頻繁的未巫,但是在release的時候要刪除這些調試代碼,那工作量是煩躁,這樣的情況下兰英,試用宏就會顯得非常的方便
//非常實用的打印,包括打印的類名——方法名-行數(shù)-打印的內容
#ifdef DEBUG
#define LRLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LRLog(...)
#endif
獲得主 window
#define Window [[UIApplication sharedApplication].delegate window]
自動退出的提示框供鸠,需要和獲得的主 window 結合使用畦贸,同時導入第三方庫 Toast
#define Toast(str) CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle]; \
[Window makeToast:str duration:0.6 position:CSToastPositionCenter style:style];\
Window.userInteractionEnabled = NO; \
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{\
Window.userInteractionEnabled = YES;\
});\
iPad Air {{0, 0}, {768, 1024}}
iPhone4s {{0, 0}, {320, 480}} 960640
iPhone5 5s {{0, 0}, {320, 568}} 1136640
iPhone6 6s {{0, 0}, {375, 667}} 1334750
iPhone6Plus 6sPlus {{0, 0}, {414, 736}} 19201080
Apple Watch 1.65inches(英寸) 320*640
獲取手機的 UUID以及屏幕的尺寸
#define UUID [[[UIDevice currentDevice] identifierForVendor] UUIDString]
#define APP_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define APP_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define TabBarH 49.0f
#define StatusBarH 20.0f
#define NavigationBarH 44.0f
// 根據(jù) iPhone6 6s 的尺寸為例
#define WIDTH APP_WIDTH/375
#define HEIGHT APP_HEIGHT/667
獲取View的屬性
#define Get_View_Width(Width) view.frame.size.width
#define Get_View_Height(Height) view.frame.size.height
#define Get_View_X(X) view.frame.origin.x
#define Get_View_Y(Y) view.frame.origin.y
分辨設備類型
//判斷是否 Retina屏、設備是否iPhone 5楞捂、是否是iPad
#define Is_Retina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
/** 判斷是否為iPhone */
#define Is_iPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
/** 判斷是否是iPad */
#define Is_iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
/** 判斷是否為iPod */
#define Is_iPod ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
/** 設備是否為iPhone 4/4S 分辨率320x480薄坏,像素640x960,@2x */
#define iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
/** 設備是否為iPhone 5C/5/5S 分辨率320x568,像素640x1136,@2x */
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
/** 設備是否為iPhone 6 分辨率375x667述呐,像素750x1334试浙,@2x */
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
/** 設備是否為iPhone 6 Plus 分辨率414x736,像素1242x2208物舒,@3x */
#define iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
設置View的圓角和邊框
#define View_Border_Radius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
由角度轉換弧度/由弧度轉換角度
#define DegreesToRadian(x) (M_PI * (x) / 180.0)
#define RadianToDegrees(radian) (radian*180.0)/(M_PI)
獲取圖片
獲取圖片資源
#define GetImage(imageName) [UIImage imageNamed:imageName]
讀取本地圖片
#define Load_Image(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
獲取文件路徑
//獲取temp
#define Path_Temp NSTemporaryDirectory()
//獲取沙盒 Document
#define Path_Document [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//獲取沙盒 Cache
#define Path_Cache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
GCD 的宏定義(需要自己加上^{})
//GCD - 一次性執(zhí)行
#define DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
//GCD - 在Main線程上運行
#define DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
//GCD - 開啟異步線程
#define DISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);
設置顏色
//設置隨機顏色
#define Random_Color [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
設置顏色和透明度
#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)]
// 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]
//帶有RGBA的顏色設置
#define Color_Alpha(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
#define Color_Clear [UIColor clearColor]
#define COLOR_BLUE_ UIColorFromRGB(0x41CEF2)
#define COLOR_GRAY_ UIColorFromRGB(0xababab) //171
#define COLOR_333 UIColorFromRGB(0x333333) //51
#define COLOR_666 UIColorFromRGB(0x666666) //102
#define COLOR_888 UIColorFromRGB(0x888888) //136
#define COLOR_999 UIColorFromRGB(0x999999) //153
#define COLOR_PLACEHOLD_ UIColorFromRGB(0xc5c5c5) //197
#define COLOR_RED_ UIColorFromRGB(0xff5400) //紅色
#define COLOR_GREEN_ UIColorFromRGB(0x31d8ab)//綠色
#define COLOR_YELLOW_ UIColorFromRGB(0xffa200)//黃色
#define COLOR_SEPARATE_LINE UIColorFromRGB(0xC8C8C8)//200
#define COLOR_LIGHTGRAY COLOR(200, 200, 200, 0.4)//淡灰色
判斷操作系統(tǒng)版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
/** 獲取系統(tǒng)版本 */
#define iOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])
/** 是否為iOS6 */
#define iOS6 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) ? YES : NO)
/** 是否為iOS7 */
#define iOS7 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) ? YES : NO)
/** 是否為iOS8 */
#define iOS8 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) ? YES : NO)
/** 是否為iOS9 */
#define iOS9 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) ? YES : NO)
NSUserDefaults 存/獲得/刪對象
//存儲對象
#define UserDefaultSetObjectForKey(__VALUE__,__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] setObject:__VALUE__ forKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}
//獲得存儲的對象
#define UserDefaultObjectForKey(__KEY__) [[NSUserDefaults standardUserDefaults] objectForKey:__KEY__]
// 刪除對象
#define UserDefaultRemoveObjectForKey(__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] removeObjectForKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}
宏定義一個彈窗方法,括號里面是方法的參數(shù)
#define ShowAlert(message) UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: @"OK"];[alert show];
與后臺連調的宏定義
//定義一個API
#define API_URL @"http://www.google.com"
//login the API 登陸API
#define API_Login [APIURL stringByAppendingString:@"Login"]
//根據(jù)不同的系統(tǒng),設置不同的動畫
#define YES_Or_NO floorf([[UIDevice currentDevice].systemVersion floatValue]) >8.0 ? YES : NO
借鑒別人的,加上自己的一些經驗闻牡,希望對大家有用,也希望大家補充