iOS 通用宏定義 高效全局宏匯總


  最近在搭建新項目,為了方便開發,常會用到一些宏定義,梳理了之前項目中用到,又查漏補缺挑選了一些網絡上比較不錯的,總結了一份分享給大家。

/***************************系統版本*****************************/

//獲取手機系統的版本

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

//是否為iOS7及以上系統

#define HitoiOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)

//是否為iOS8及以上系統

#define HitoiOS8 ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0)

//是否為iOS9及以上系統

#define HitoiOS9 ([[UIDevice currentDevice].systemVersion doubleValue] >= 9.0)

//是否為iOS10及以上系統

#define HitoiOS10 ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0)

//是否為iOS11及以上系統

#define HitoiOS11 ([[UIDevice currentDevice].systemVersion doubleValue] >= 11.0)

 

/***************************沙盒路徑*****************************/

//沙盒路徑

#define HitoHomePath NSHomeDirectory()

//獲取沙盒 Document

#define HitoPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

//獲取沙盒 Cache

#define HitoPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

//獲取沙盒 temp

#define HitoPathTemp NSTemporaryDirectory()

 

/***************************打印日志*****************************/

//輸出語句

#ifdef DEBUG

# define NSLog(FORMAT, ...) printf("[%s<%p>行號:%d]:\n%s\n",__FUNCTION__,self,__LINE__,[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

#else

# define NSLog(FORMAT, ...) 

#endif

 

/***************************系統高度*****************************/

//屏幕的寬高

#define HitoScreenW [UIScreen mainScreen].bounds.size.width

#define HitoScreenH [UIScreen mainScreen].bounds.size.height

//屏幕大小

#define HitoScreenSize [UIScreen mainScreen].bounds

//比例寬和高(以6s為除數)

#define HitoActureHeight(height)  roundf(height/375.0 * HitoScreenW)

#define HitoActureWidth(Width)  roundf(Width/667.0 * HitoScreenH)

//狀態欄的高度

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

//導航欄+狀態欄的高度

#define HitoTopHeight (HitoStatusBarHeight + HitoNavBarHeight)

 

/***************************視圖,類初始化*****************************/

//property屬性快速聲明

#define HitoPropertyString(s)      @property(nonatomic,copy)NSString * s

#define HitoPropertyNSInteger(s)   @property(nonatomic,assign)NSIntegers

#define HitoPropertyFloat(s)       @property(nonatomic,assign)floats

#define HitoPropertyLongLong(s)    @property(nonatomic,assign)long long s

#define HitoPropertyNSDictionary(s)@property(nonatomic,strong)NSDictionary * s

#define HitoPropertyNSArray(s)     @property(nonatomic,strong)NSArray * s

#define HitoPropertyNSMutableArray(s)    @property(nonatomic,strong)NSMutableArray * s

 

//獲取視圖寬高XY等信息

#define HitoviewH(view1) view1.frame.size.height

#define HitoviewW(view1) view1.frame.size.width

#define HitoviewX(view1) view1.frame.origin.x

#define HitoviewY(view1) view1.frame.origin.y

//獲取self.view的寬高

#define HitoSelfViewW (self.view.frame.size.width)

#define HitoSelfViewH (self.view.frame.size.height)

///實例化

#define HitoViewAlloc(view,x,y,w,h) [[view alloc]initWithFrame:CGRectMake(x, y, w, h)]

#define HitoAllocInit(Controller,cName) Controller *cName = [[Controller alloc]init]

 

//View圓角和加邊框

#define HitoViewBorderRadius(View,Radius,Width,Color)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES];\

[View.layer setBorderWidth:(Width)];\

[View.layer setBorderColor:[Color CGColor]]

 

// View圓角

#define HitoViewRadius(View,Radius)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES]

 

/***************************圖片,顏色,字號*****************************/

//默認圖片

#define HitoPlaceholderImage [UIImage imageNamed:@"XXX"]

//定義UIImage對象

#define HitoImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

//基本顏色

#define HitoClearColor [UIColor clearColor]

#define HitoWhiteColor [UIColor whiteColor]

#define HitoBlackColor [UIColor blackColor]

#define HitoGrayColor [UIColor grayColor]

#define HitoGray2Color [UIColor lightGrayColor]

#define HitoBlueColor [UIColor blueColor]

#define HitoRedColor [UIColor redColor]

///顏色 a代表透明度,1為不透明,0為透明

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

// rgb顏色轉換(16進制->10進制)

#define HitoColorFromRGB(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 LineColor [UIColor colorWithRed:201/255.0 green:201/255.0 blue:201/255.0 alpha:0.2]

//加粗

#define HitoBoldSystemFontOfSize(FONTSIZE) [UIFont boldSystemFontOfSize:FONTSIZE]

//字號

#define HitoSystemFontOfSize(FONTSIZE)[UIFont systemFontOfSize:FONTSIZE]

 

/***************************通知和本地存儲*****************************/

//創建通知

#define HitoAddNotification(selectorName,key) [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selectorName) name:key object:nil];

//發送通知

#define HitoSendNotification(key) [[NSNotificationCenter defaultCenter] postNotificationName:key object:self userInfo:nil];

//移除通知

#define HitoRemoveNotification(key) [[NSNotificationCenter defaultCenter]removeObserver:self name:key object:nil];

//本地化存儲

#define HitoUserDefaults(NSUserDefaults,defu) NSUserDefaults * defu = [NSUserDefaults standardUserDefaults];

 

/***************************其他*****************************/

//主窗口

#define HitoApplication [UIApplication sharedApplication].keyWindow

//字符串拼接

#define HitoStringWithFormat(format,...)[NSString stringWithFormat:format,##__VA_ARGS__]

//GCD代碼只執行一次

#define HitoDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);

//強引用

#define HitoWeakSelf __weak typeof(self)WeakSelf = self;

//成功標識

#define HitoSuccess @"success"

//失敗標識

#define HitoFailure @"failure"

//登錄狀態標識

#define HitoSucTitle @"登錄成功"

#define HitoFaiTitle @"登錄失敗"

//網絡狀態標識

#define HitoFaiNetwork @"網絡錯誤"

  如有雷同純屬巧合,如有錯誤望指正。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM