前言
今天將一些簡化工程代碼的宏定義拿出來分享一下,如果大家也有簡化代碼的相關經驗也可以一起來交流。
Application
#define APPLICATION [UIApplication sharedApplication]
#define APPDLE (AppDelegate*)[APPLICATION delegate]
布局相關
在純代碼的工程中,由于oc語法本身并不是很簡潔的特性,控件布局會占有很大篇幅的代碼,如果將一些位置、大小、中心、間隙等寫成簡短明了的宏定義,就會大大增加代碼的可讀性(當然Masonry框架也是一個不錯的選擇)。
- Frame相關
傳參frame
對象,獲取frame
的相關屬性值
#define FRAME_ORIGIN(aFrame) ((aFrame).origin)
#define FRAME_X(aFrame) ((aFrame).origin.x)
#define FRAME_Y(aFrame) ((aFrame).origin.y)
#define FRAME_SIZE(aFrame) ((aFrame).size)
#define FRAME_HEIGHT(aFrame) ((aFrame).size.height)
#define FRAME_WIDTH(aFrame) ((aFrame).size.width)
/*修改frame對象的x、y、width、height屬性值*/
#define FRAME_CHANGE_X(aFrame,x) CGRectMake(x, (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
#define FRAME_CHANGE_Y(aFrame,y) CGRectMake((aFrame).origin.x, y, (aFrame).size.width, (aFrame).size.height)
#define FRAME_CHANGE_WIDTH(aFrame,w) CGRectMake((aFrame).origin.x, (aFrame).origin.y, w, (aFrame).size.height)
#define FRAME_CHANGE_HEIGHT(aFrame,h) CGRectMake((aFrame).origin.x, (aFrame).origin.y, (aFrame).size.width, h)
// 向左移動offset位移后得到frame對象
#define FRAME_MOVE_Left(aFrame,offset) CGRectMake((aFrame).origin.x-(offset), (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
// 向右移動offset位移后得到frame對象
#define FRAME_MOVE_Right(aFrame,offset) CGRectMake((aFrame).origin.x+(offset), (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
// 向上移動offset位移后得到frame對象
#define FRAME_MOVE_Up(aFrame,offset) CGRectMake((aFrame).origin.x, (aFrame).origin.y-(offset), (aFrame).size.width, (aFrame).size.height)
// 向下移動offset位移后得到frame對象
#define FRAME_MOVE_Down(aFrame,offset) CGRectMake((aFrame).origin.x, (aFrame).origin.y+(offset), (aFrame).size.width, (aFrame).size.height)
傳參view
對象,獲取view
的frame
、bounds
相關屬性值
#define VIEW_BOUNDS(aView) ((aView).bounds)
#define VIEW_FRAME(aView) ((aView).frame)
#define VIEW_ORIGIN(aView) ((aView).frame.origin)
#define VIEW_X(aView) ((aView).frame.origin.x)
#define VIEW_Y(aView) ((aView).frame.origin.y)
#define VIEW_SIZE(aView) ((aView).frame.size)
#define VIEW_HEIGHT(aView) ((aView).frame.size.height) // 視圖高度
#define VIEW_WIDTH(aView) ((aView).frame.size.width) // 視圖寬度
其實本來以下兩個宏按照語義想要寫成VIEW_Right_X(aView)
、
VIEW_Bottom_Y(aView)
,但是由于XCode的自動提示,寫成以下形式就會在自動提示列表中與VIEW_X(aView)
、VIEW_Y(aView)
兩個宏并列,更方便選擇
#define VIEW_X_Right(aView) ((aView).frame.origin.x + (aView).frame.size.width) // 視圖右邊界x坐標
#define VIEW_Y_Bottom(aView) ((aView).frame.origin.y + (aView).frame.size.height) // 視圖底部y坐標
- Center相關
#define VIEW_CENTER(aView) ((aView).center)
#define VIEW_CENTER_X(aView) ((aView).center.x)
#define VIEW_CENTER_Y(aView) ((aView).center.y)
- 間距
有人會說定義這樣一個宏并不是簡化代碼反而增加了代碼,確實如此。我們在布局控件的時候經常會涉及+、-一些間距值,當我們回過頭閱讀那成百上千行代碼中+、-的那些值的時候,我們就很難去猜測它們的意義。如果用這樣一個宏去表示,就大大增加了代碼的可讀性。
#define Space_(space) (space) // 表示整形、浮點型間距
屏幕坐標、尺寸相關
// 狀態欄占用高度
#define StateBarHeight 20.f
// 狀態欄底部y坐標
#define OffsetStateBarHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? StateBarHeight : 0.f)
// 頂部狀態欄占用高度
#define TopNavBarHeight 40.f
// 頂部導航欄底部y坐標
#define OffsetTopNavBarHeight (OffsetStateBarHeight + TopNavBarHeight)
// 底部導航欄占用高度
#define BottomTabBarHeight 40.f
// 屏幕高度
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
// 屏幕寬度
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
// 屏幕可操作高度
#define MainHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? ScreenHeight : (ScreenHeight - StateBarHeight))
// 屏幕可操作寬度
#define MainWidth ScreenWidth
// 去除上下導航欄剩余中間視圖高度
#define ContentHeight (MainHeight -OffsetTopNavBarHeight -BottomTabBarHeight)
設備系統相關
- 設備
這只列出了目前已有的iPhone屏幕尺寸
// x
#define IS_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
// 6P、6sP、7P、8P
#define IS_iPhone678_Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
// 6、6s、7、8
#define IS_iPhone678 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
// 5、5s
#define IS_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
// 3g、4、4s
#define IS_iPhone34 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
// 是否是Retina屏幕
#define IS_Retina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? [[UIScreen mainScreen] currentMode].size.width > 320 : NO)
- 系統相關
// app版本號
#define DEVICE_APP_VERSION (NSString *)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
// app Build版本號
#define DEVICE_APP_BUILD (NSString *)[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
// 系統版本號(string)
#define DEVICE_OS_VERSION [[UIDevice currentDevice] systemVersion]
// 系統版本號(float)
#define DEVICE_OS_VERSION_VALUE [DEVICE_OS_VERSION floatValue]
#define IS_IOSVersion_4 (DEVICE_OS_VERSION_VALUE >= 4.0 && DEVICE_OS_VERSION_VALUE < 5.0)
#define IS_IOSVersion_5 (DEVICE_OS_VERSION_VALUE >= 5.0 && DEVICE_OS_VERSION_VALUE < 6.0)
#define IS_IOSVersion_6 (DEVICE_OS_VERSION_VALUE >= 6.0 && DEVICE_OS_VERSION_VALUE < 7.0)
#define IS_IOSVersion_7 (DEVICE_OS_VERSION_VALUE >= 7.0 && DEVICE_OS_VERSION_VALUE < 8.0)
#define IS_IOSVersion_8 (DEVICE_OS_VERSION_VALUE >= 8.0 && DEVICE_OS_VERSION_VALUE < 9.0)
#define IS_IOSVersion_9 (DEVICE_OS_VERSION_VALUE >= 9.0 && DEVICE_OS_VERSION_VALUE < 10.0)
#define IS_IOSVersion_10 (DEVICE_OS_VERSION_VALUE >= 10.0 && DEVICE_OS_VERSION_VALUE < 11.0)
#define IS_IOSVersion_11 (DEVICE_OS_VERSION_VALUE >= 11.0 && DEVICE_OS_VERSION_VALUE < 12.0)
字體相關
- 字體
#define FONT_SIZE(f) [UIFont systemFontOfSize:(f)]
#define FONT_BOLD_SIZE(f) [UIFont boldSystemFontOfSize:(f)]
#define FONT_ITALIC_SIZE(f) [UIFont italicSystemFontOfSize:(f)]
- 大小屏字體自動切換
有的應用希望有一個好的用戶體驗會在不同的屏幕上適配不同大小字體,這時就可以使用以下的宏定義來實現。但是如果應用中字體大小不能做到全局統一,就不要使用以下的宏定義來實現字體大小適配。這個就看你所開發的具體情況了,例如:
#define IS_SmallScreen (IS_iPhone5 || IS_iPhone4)
#define MaxFontSize (IS_SmallScreen ? 21.f : 25.f )
#define LagerFontSize (IS_SmallScreen ? 17.f : 19.f )
#define BigFontSize (IS_SmallScreen ? 15.f : 17.f )
#define NormalFontSize (IS_SmallScreen ? 13.f : 15.f )
#define SmallFontSize (IS_SmallScreen ? 11.f : 13.f )
#define MinFontSize (IS_SmallScreen ? 9.f : 11.f )
顏色相關
- 系統顏色
#define COLOR_Clear [UIColor clearColor]
#define COLOR_White [UIColor whiteColor]
#define COLOR_Black [UIColor blackColor]
#define COLOR_Red [UIColor redColor]
#define COLOR_DarkGray [UIColor darkGrayColor]
#define COLOR_LightGray [UIColor lightGrayColor]
#define COLOR_GrayColor [UIColor grayColor]
#define COLOR_Green [UIColor greenColor]
#define COLOR_BlueColor [UIColor blueColor]
#define COLOR_Cyan [UIColor cyanColor]
#define COLOR_Yellow [UIColor yellowColor]
#define COLOR_Magenta [UIColor magentaColor]
#define COLOR_Orange [UIColor orangeColor]
#define COLOR_Purple [UIColor purpleColor]
#define COLOR_Brown [UIColor brownColor]
- 顏色轉換
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:(a)]
#define HexCOLOR(HexStr) [UIColor colorWithHexString:HexStr]
+ (UIColor *)colorWithHexString:(NSString *)color{
return [self colorWithHexString:color alpha:1.0f];
}
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// strip 0X if it appears
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alpha];
}
字符串相關
#define StrOfInterger(interger) [NSString stringWithFormat:@"%ld",(long)(interger)]
#define StrOfFloat(float) [NSString stringWithFormat:@"%f",(float)]
Image相關
#define IMG_Name(imgName) [UIImage imageNamed:(imgName)]
#define IMG_ImgWidth(img) ((img).size.width)
#define IMG_ImgHeight(img) ((img).size.height)
校驗相關
#define STRINGHASVALUE(str) (str && [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0)
#define IsCanUseString(str) ((str != nil) && ![str isKindOfClass:[NSNull class]] && [str isKindOfClass:[NSString class]] && [str length] > 0 )
#define IsCanUseArray(arr) ( arr && (arr != nil) && ![arr isKindOfClass:[NSNull class]] )
#define IsCanUseDic(dic) ( dic && (dic != nil) && ![dic isKindOfClass:[NSNull class]] )
#define IsCanUseObj(obj) ( obj && (obj != nil) && ![obj isKindOfClass:[NSNull class]] )
#define IsNullClass(class) [class isKindOfClass:[NSNull class]]
打印相關
// mark(NSString類型參數)為打印內容標題
#define NSLOG_Str(mark,str) NSLog(@"##%@##--str:%@--",(mark),(str))
#define NSLOG_Int(mark,int) NSLog(@"##%@##--int:%ld--",(mark),(int))
#define NSLOG_Float(mark,float) NSLog(@"##%@##--float:%f--",(mark),(float))
#define NSLOG_Bool(mark,bool) NSLog(@"##%@##--bool:%@--",(mark),(bool) ? @"YES" : @"NO")
#define NSLOG_Point(mark,point) NSLog(@"##%@##--x:%f--y:%f--",(mark),(point).x,(point).y)
#define NSLOG_Size(mark,size) NSLog(@"##%@##--width:%f--height:%f--",(mark),(size).width,(size).height)
#define NSLOG_Frame(mark,frame) NSLog(@"##%@##--x:%f--y:%f--width:%f--height:%f--",(mark),(frame).origin.x,(frame).origin.y,(frame).size.width,(frame).size.height)
版權聲明:出自MajorLMJ技術博客的原創作品 ,轉載時必須注明出處及相應鏈接!