iOS开发常用宏

182 阅读1分钟

#ifndef Macro_h #define Macro_h

//设备

#define kAppCurrentVersion [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] #define kAppCurrentBuildVersion [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] #define kAppKeyWindow [UIApplication sharedApplication].keyWindow #define kMainScreenBounds [UIScreen mainScreen].bounds

#define kScreenHeight CGRectGetHeight([UIScreen mainScreen].bounds) #define kScreenWidth CGRectGetWidth([UIScreen mainScreen].bounds)

#define kScreenIphone5 CGRectGetHeight([UIScreen mainScreen].bounds) == 568 #define kScreenIphonePlus CGRectGetHeight([UIScreen mainScreen].bounds) == 736 #define kScreenIphoneX_Model kStatusBarHeight > 20

#define kStatusBarHeight CGRectGetHeight([UIApplication sharedApplication].statusBarFrame) #define kNavBarHeight 44 #define kNavBarFrameMaxY (kStatusBarHeight + kNavBarHeight)

#define kBottomSafeMargin (kStatusBarHeight==20 ? 0 : 34)

#define kBottomDefaultMargin(padding) (kBottomSafeMargin > 0 ? kBottomSafeMargin : padding)

//系统 #define kUserDefaults [NSUserDefaults standardUserDefaults] #define kNotificationdDefaultCenter [NSNotificationCenter defaultCenter]

// 字体 #define kFontMedium(font) [UIFont systemFontOfSize:font weight:UIFontWeightMedium]

#define kFontRegular(font) [UIFont systemFontOfSize:font]

// 颜色

#define kMainLightColor [UIColor colorWithHexString:@"#FFFBE8"] #define kMainYellowColor [UIColor colorWithHexString:@"#FAAB0C"] #define kMainOrangeColor [UIColor colorWithHexString:@"#FF7B1B"] #define kMainGreenColor [UIColor colorWithHexString:@"#07C160"] #define kMainBlueColor [UIColor colorWithHexString:@"#45A9F1"] #define kMainRedColor [UIColor colorWithHexString:@"#EB3030"]

/// 主题色 #define kMainThemeColor [UIColor colorWithHexString:@"#FF5000"]

/// 背景灰、线(白色背景下)#F5F5F5 #define kMainBgColor [UIColor colorWithHexString:@"#F5F5F5"]

/// 线(深色背景下)#EEEEEE #define kMainLightLine [UIColor colorWithHexString:@"#EEEEEE"]

/// 线(深色背景下)#E5E5E5 #define kMainDarkLine [UIColor colorWithHexString:@"#E5E5E5"]

/// 辅助、说明文字 #999999 #define kMainLightGray [UIColor colorWithHexString:@"#999999"]

/// 主文字黑(次级)#666666 #define kMainDarkGray [UIColor colorWithHexString:@"#666666"]

/// 主文字黑(一级)#333333 #define kMainBlack [UIColor colorWithHexString:@"#333333"]

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

//#define kRandomColor [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0]

//打印 #ifdef DEBUG

#define YLZLog(...) printf("[%s] %s [第%d行]: %s\n", TIME ,PRETTY_FUNCTION ,LINE, [[NSString stringWithFormat:VA_ARGS] UTF8String]) #else #define YLZLog(...) #endif

// 其他

#define kWeakSelf __weak typeof(self) weakself = self;

#define kBack_queue_action(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)

#define kMain_queue_action(block) dispatch_async(dispatch_get_main_queue(),block)

#define kDelay_action(time, block) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), block);

#define kLayerRadius(View, Radius)

[View.layer setCornerRadius:(Radius)];
[View.layer setMasksToBounds:YES]

#define kLayerBorder(View, Width, Color)

[View.layer setBorderWidth:(Width)];
[View.layer setBorderColor:(Color).CGColor];

#define kLayerShadow(View, Radius)

View.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
View.layer.cornerRadius = Radius;
View.layer.shadowRadius = Radius + 4;
[View.layer setShadowOpacity:1.0];
[View.layer setShadowOffset:CGSizeMake(0.0f, 2.0f)];
[View.layer setMasksToBounds:NO];

#define SINGLETON_FOR_HEADER(className)
\

  • (className *)shared##className;

#define SINGLETON_FOR_CLASS(className)
\

  • (className *)shared##className {
    static className *shared##className = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    shared##className = [[self alloc] init];
    });
    return shared##className;
    }

#endif /* Macro_h */