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

來源:互聯網
上載者:User

標籤:nss   home   printf   for   shared   type   新項目   對象   導覽列   

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

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

//擷取手機系統的版本

#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 @"網路錯誤"

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

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.