分享一個自己整理的常用宏,有摘抄網絡,有自己寫的,不定時更新
v1.0 2016-08-10
//
// PrefixHeader.pch
// EJW-IOS
//
// Created by iroycn
//
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
//常用字符
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
//<><><>獲取屏幕高寬<><><><><>(註釋的是支持橫豎屏,但我們項目裏沒有用到,所以註釋,只支持ios8以上)
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 當前Xcode支持iOS8及以上
//#define kSCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
//#define kSCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
//#define kSCREEN_SIZE ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale):[UIScreen mainScreen].bounds.size)
//#else
#define kSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define kSCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
#define kSCREEN_SIZE [UIScreen mainScreen].bounds.size
//#endif
//<><><><><><><><><><><><><>
//<><><>緩存<><><><><>
#import "TMCache.h"
#define kSetCache(key,value) [[TMCache sharedCache] setObject:value forKey:key];
#define kGetCache(key) [[TMCache sharedCache] objectForKey:key];
#define kRemoveCache(key) [[TMCache sharedCache] removeObjectForKey:key];
#define kRemoveAllCache [[TMCache sharedCache] removeAllObjects];
////////////////////////////////////////////////////////////////////
//<><><>獲取通知中心<><><><><>
#define kNotificationCenter [NSNotificationCenter defaultCenter]
////////////////////////////////////////////////////////////////////
//<><><>設置隨機顏色<><><><><>
#define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
////////////////////////////////////////////////////////////////////
//<><><>設置RGB顏色/設置RGBA顏色<><><><><>
//RGB色系
#define kColorA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
#define kColor(r, g, b) kColorA(r, g, b, 1.0)
//16進制 ->#ffffff
#define kColorHexA(hexValue,a) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:a]
#define kColorHex(hexValue) kColorHexA(hexValue,1.0)
//清除
#define kClearColor [UIColor clearColor]
////////////////////////////////////////////////////////////////////
//<><><>自定義高效率的 NSLog<><><><><>
#ifdef DEBUG
#define kLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define kLog(...)
#endif
////////////////////////////////////////////////////////////////////
//<><><>弱引用/強引用<><><><><>
#define kWeakSelf(type) __weak typeof(type) weak##type = type;
#define kStrongSelf(type) __strong typeof(type) type = weak##type;
////////////////////////////////////////////////////////////////////
//<><><>設置 view 圓角和邊框<><><><><>
#define kBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
////////////////////////////////////////////////////////////////////
#import "MBProgressHUD.h"
//<><><>設置加載提示框(第三方框架:MBProgressHUD)<><><><><>
// 加載
#define kShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
// 收起加載
#define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
// 設置加載
#define NetworkActivityIndicatorVisible(x) [UIApplication sharedApplication].networkActivityIndicatorVisible = x
//window對象
#define kWindow [UIApplication sharedApplication].keyWindow
#define kBackView for (UIView *item in kWindow.subviews) { \
if(item.tag == 10000) \
{ \
[item removeFromSuperview]; \
UIView * aView = [[UIView alloc] init]; \
aView.frame = [UIScreen mainScreen].bounds; \
aView.tag = 10000; \
aView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1]; \
[kWindow addSubview:aView]; \
} \
} \
//MB,在window層顯示
#define kShowHUDAndActivity kBackView;[MBProgressHUD showHUDAddedTo:kWindow animated:YES];kShowNetworkActivityIndicator()
#define kHiddenHUD [MBProgressHUD hideAllHUDsForView:kWindow animated:YES]
#define kRemoveBackView for (UIView *item in kWindow.subviews) { \
if(item.tag == 10000) \
{ \
[UIView animateWithDuration:0.4 animations:^{ \
item.alpha = 0.0; \
} completion:^(BOOL finished) { \
[item removeFromSuperview]; \
}]; \
} \
} \
#define kHiddenHUDAndAvtivity kRemoveBackView;kHiddenHUD;HideNetworkActivityIndicator()
//>>>>>>>>>>>>>>>>>>>>>>>>>
//<><><><><><><><><><>顯示加載菊花
//我自己寫的,顯示的位置
#define kShowHUD(view) kBackView;[MBProgressHUD showHUDAddedTo:view animated:YES];kShowNetworkActivityIndicator()
//隱藏
#define kHideHUD(view) [MBProgressHUD hideAllHUDsForView:view animated:YES]
//<><><><><><><><><><>顯示問題提醒
#define kShowLabel(view,text) MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];\
hud.labelText = text;\
hud.mode = MBProgressHUDModeText;\
hud.removeFromSuperViewOnHide = YES;\
[hud hide:YES afterDelay:0.7];\
////////////////////////////////////////////////////////////////////
//<><><>獲取圖片資源<><><><><>
//讀取本地圖片
#define kLoadImage(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
//定義UIImage對象
#define kIMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
//獲取圖片;前面2種性能比這個高,但這個常用,assets.xcassets裏的用這個吧
#define kImageName(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
////////////////////////////////////////////////////////////////////
//<><><>獲取系統版本<><><><><>
//獲取當前版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//判斷 iOS 8 或更高的系統版本
//#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
//等於
#define IOS_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
//大於
#define IOS_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
//大於等於
#define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
//小於
#define IOS_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
//小於等於
#define IOS_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
////////////////////////////////////////////////////////////////////
//<><><>判斷是真機還是模擬器<><><><><>
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
////////////////////////////////////////////////////////////////////
//<><><>沙盒目錄文件<><><><><>
//獲取temp
#define kPathTemp NSTemporaryDirectory()
//獲取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//獲取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
////////////////////////////////////////////////////////////////////
//<><><>Frame操作相關<><><><><>/
//獲取垂直居中的x(父的高度/2-子的高度/2)
#define CENTER_VERTICALLY(parent,child) floor((parent.frame.size.height - child.frame.size.height) / 2)
//獲取水平居中的y(父的寬度/2-子的寬度/2)
#define CENTER_HORIZONTALLY(parent,child) floor((parent.frame.size.width - child.frame.size.width) / 2)
// example: [[UIView alloc] initWithFrame:(CGRect){CENTER_IN_PARENT(parentView,500,500),CGSizeMake(500,500)}];
#define CENTER_IN_PARENT(parent,childWidth,childHeight) CGPointMake(floor((parent.frame.size.width - childWidth) / 2),floor((parent.frame.size.height - childHeight) / 2))
#define CENTER_IN_PARENT_X(parent,childWidth) floor((parent.frame.size.width - childWidth) / 2)
#define CENTER_IN_PARENT_Y(parent,childHeight) floor((parent.frame.size.height - childHeight) / 2)
//view的寬度
#define WIDTH(view) view.frame.size.width
//view的高度
#define HEIGHT(view) view.frame.size.height
//view的x
#define X(view) view.frame.origin.x
//view的y
#define Y(view) view.frame.origin.y
//view的x
#define LEFT(view) view.frame.origin.x
//view的y
#define TOP(view) view.frame.origin.y
//view的bottom的y
#define BOTTOM(view) (view.frame.origin.y + view.frame.size.height)
//view的right的x
#define RIGHT(view) (view.frame.origin.x + view.frame.size.width)
////////////////////////////////////////////////////////////////////
//<><><>定義UIFont,默認system<><><><><>
#define FONT(F) [UIFont systemFontOfSize:F]
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]
////////////////////////////////////////////////////////////////////
//<><>初始化一個普通的alert view<><><><><>
#define kALERT(info)\
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:info delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];\
[alert show];
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
////////////////////////////////////////////////////////////////////
//<><><NSUserDefaults 實例化<><><><><>
////////////////////////////////////////////////////////////////////
//是否爲empty
static inline BOOL IsEmpty(id thing) {
return thing == nil || [thing isEqual:[NSNull null]]
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}
//未知字符串(nil)轉爲字符串(非nil);在不確定字符串是否爲nil的情況下使用
static inline NSString *StringFromObject(id object) {
if (object == nil || [object isEqual:[NSNull null]]) {
return @"";
} else if ([object isKindOfClass:[NSString class]]) {
return object;
} else if ([object respondsToSelector:@selector(stringValue)]){
return [object stringValue];
} else {
return [object description];
}
}
#endif /* PrefixHeader_pch */