//主窗口
#define KeyWindow \
[UIApplication sharedApplication].keyWindow
//屏幕宽度
#define ScreenWidth \
[UIScreen mainScreen].bounds.size.width
//屏幕高度
#define ScreenHeight \
[UIScreen mainScreen].bounds.size.height
/// 适配相关的动态宽度高度
#define WidthScale(number) ([UIScreen mainScreen].bounds.size.width/375*(number))
#define HeightScale(number) ([UIScreen mainScreen].bounds.size.height/667*(number))
#define WidthScaleWith6Plus(number) ([UIScreen mainScreen].bounds.size.width/1080*(number))
#define HeightScaleWith6Plus(number) ([UIScreen mainScreen].bounds.size.height/1920*(number))
//1像素
#define ScreenLine \
(1 / [UIScreen mainScreen].scale)
/** 适配补充 */
/// 神圣的 iPhone X
#define IS_iPhoneX (isIPhoneXSeries())
static inline BOOL isIPhoneXSeries() {
BOOL iPhoneXSeries = NO;
if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
return iPhoneXSeries;
}
if (@available(iOS 11.0, *)) {
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
if (mainWindow.safeAreaInsets.bottom > 0.0) {
iPhoneXSeries = YES;
}
}
return iPhoneXSeries;
}
/// 导航栏高度
#define NavigationHeight (IS_iPhoneX ? 88 : 64)
/// 状态栏
#define StatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
/// 安全的底部工具条高度增加量
#define SafeTabHeightOffset (IS_iPhoneX ? 34 : 0)
/** 通知相关 */
//通知中心发送通知
#define POST_NOTIFICATION_(SY_Name, SY_UserInfo) \
[[NSNotificationCenter defaultCenter] postNotificationName:SY_Name object:nil userInfo:(SY_UserInfo)]
//通知中心接收通知
#define RECEIVE_NOTIFICATION_(SY_SEL, SY_name) \
[[NSNotificationCenter defaultCenter] addObserver:self selector:SY_SEL name:SY_name object:nil]
#define RECEIVE_NOTIFICATION1_(SY_SEL, SY_name, SY_object) \
[[NSNotificationCenter defaultCenter] addObserver:self selector:SY_SEL name:SY_name object:nil]
//移除通知
#define REMOVE_NOTIFICATION \
[[NSNotificationCenter defaultCenter] removeObserver:self]
//弱引用
#define WeakSelf(vc) \
__weak typeof(vc *) weakSelf = self
//const字符串
#define NSString_Const_H_(SY_strName) \
UIKIT_EXTERN NSString * const SY_strName;
#define NSString_Const_M_(SY_strName, SY_String) \
NSString * const SY_strName = SY_String;
#define NSUserDefaultsSave_(key,value) \
[[NSUserDefaults standardUserDefaults] setObject:value forKey:key] //以key,value存储信息
#define NSUserDefaultsGet_(key) \
[[NSUserDefaults standardUserDefaults] objectForKey:key] //以key取出value
#define NSUserDefaultsRemove_(key) \
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key] //以key删除value
//立即同步
#define NSUserDefaultsSync \
[[NSUserDefaults standardUserDefaults] synchronize]
/** 颜色 */
#define RGB(hexValue) RGBA(hexValue, 1.0)
#define RGBA(hexValue, a) [UIColor colorWithRed:((CGFloat)((hexValue & 0xFF0000) >> 16))/255.0 green:((CGFloat)((hexValue & 0xFF00) >> 8))/255.0 blue:((CGFloat)(hexValue & 0xFF))/255.0 alpha:a]
//RGB颜色
#define UIColorWithRGB_(SY_R,SY_G,SY_B) \
[UIColor colorWithRed:(SY_R/255.0) green:(SY_G/255.0) blue:(SY_B/255.0) alpha:1]
//随机颜色
#define RandomColor \
[UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
//十六进制颜色
#define UIColorWithHex16_(SY_0Xefefef) \
[UIColor colorWithRed:((SY_0Xefefef & 0xFF0000) >> 16) / 255.0 green:((SY_0Xefefef & 0x00FF00) >> 8) / 255.0 blue:((SY_0Xefefef & 0x0000FF)) / 255.0 alpha:1]
#pragma mark === GCD ====
// 宏定义 回到主线程
#ifndef an_dispatch_main_async_safe
#define an_dispatch_main_async_safe(block)\
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}
#endif
#pragma mark =========== 常用字体===============
/// 细体
#define FontLight(fontSize) [UIFont fontWithName:@"PingFangSC-Light" size:fontSize] ? : [UIFont systemFontOfSize:fontSize]
/// 常规字体
#define FontRegular(fontSize) [UIFont fontWithName:@"PingFangSC-Regular" size:fontSize] ? : [UIFont systemFontOfSize:fontSize]
/// 加粗字体、中等字体
#define FontMedium(fontSize) [UIFont fontWithName:@"PingFangSC-Medium" size:fontSize] ? : [UIFont boldSystemFontOfSize:fontSize]
#pragma mark - 定义属性
/**
* 便捷定义@property属性
*/
/** copy */
//NSString
#define String_(name) \
SY_copy_property(NSString*,name)
//NSArray
#define Array_(name) \
SY_copy_property(NSArray*,name)
//NSDictionary
#define Dictionary_(name) \
SY_copy_property(NSDictionary*,name)
//NSNumber,它没用对应的不可变类,其实用copy或strong没有区别
#define Number_(name) \
SY_copy_property(NSNumber*,name)
//NSData
#define Data_(name) \
SY_copy_property(NSData*,name)
//NSSet
#define Set_(name) \
SY_copy_property(NSSet*,name)
//NSIndexSet
#define IndexSet_(name) \
SY_copy_property(NSIndexSet*,name)
//带多态的数组和可变数组
#define mArray_type(modelType, name) \
@property(nonatomic, strong)NSMutableArray <modelType *> *name;
#define Array_type(modelType, name) \
@property(nonatomic, strong)NSArray <modelType *> *name;
//代码块,名称和传参,没有传参就不填
#define Block_(name,...) \
SY_set_block(void,name,__VA_ARGS__)
//有返回值的代码块
#define BlockReturn_(name,returnClass,...) \
SY_set_block(returnClass,name,__VA_ARGS__)
/** strong */
//UISearchBar
#define SearchBar_(SY_name) \
@property (nonatomic, strong) UISearchBar *SY_name;
//UICollectionViewFlowLayout
#define FlowLayout_(SY_name) \
@property(nonatomic, strong)UICollectionViewFlowLayout *SY_name;
//NSMutableString
#define mString_(name,...) \
SY_strong_property(NSMutableString*,name,__VA_ARGS__)
//NSMutableArray
#define mArray_(name,...) \
SY_strong_property(NSMutableArray*,name,__VA_ARGS__)
//NSMutableDictionary
#define mDictionary_(name,...) \
SY_strong_property(NSMutableDictionary*,name,__VA_ARGS__)
//NSMutableData
#define mData_(name,...) \
SY_strong_property(NSMutableData*,name,__VA_ARGS__)
//NSMutableSet
#define mSet_(name,...) \
SY_strong_property(NSMutableSet*,name,__VA_ARGS__)
//NSMutableIndexSet
#define mIndexSet_(name,...) \
SY_strong_property(NSMutableIndexSet*,name,__VA_ARGS__)
//UIImage
#define Image_(name) \
SY_strong_property(UIImage*,name)
//UIColor
#define Color_(name) \
SY_strong_property(UIColor*,name)
//id
#define id_(name,...) \
SY_strong_property(id,name,__VA_ARGS__)
//UIView
#define View_(name,...) \
SY_strong_property(UIView*,name,__VA_ARGS__)
//UIImageView
#define ImageView_(name,...) \
SY_strong_property(UIImageView*,name,__VA_ARGS__)
//UILabel
#define Label_(name,...) \
SY_strong_property(UILabel*,name,__VA_ARGS__)
//UIButton
#define Button_(name,...) \
SY_strong_property(UIButton*,name,__VA_ARGS__)
//UITableView
#define TableView_(name,...) \
SY_strong_property(UITableView*,name,__VA_ARGS__)
//UICollectionView
#define CollectionView_(name,...) \
SY_strong_property(UICollectionView*,name,__VA_ARGS__)
//UISegmentedControl
#define SegmentedControl_(name,...) \
SY_strong_property(UISegmentedControl*,name,__VA_ARGS__)
//UITextField
#define TextField_(name,...) \
SY_strong_property(UITextField*,name,__VA_ARGS__)
//UISlider
#define Slider_(name,...) \
SY_strong_property(UISlider*,name,__VA_ARGS__)
//UISwitch
#define Switch_(name,...) \
SY_strong_property(UISwitch*,name,__VA_ARGS__)
//UIActivityIndicatorView
#define ActivityIndicatorView_(name,...) \
SY_strong_property(UIActivityIndicatorView*,name,__VA_ARGS__)
//UIProgressView
#define ProgressView_(name,...) \
SY_strong_property(UIProgressView*,name,__VA_ARGS__)
//UIPageControl
#define PageControl_(name,...) \
SY_strong_property(UIPageControl*,name,__VA_ARGS__)
//UIStepper
#define Stepper_(name,...) \
SY_strong_property(UIStepper*,name,__VA_ARGS__)
//UITextView
#define TextView_(name,...) \
SY_strong_property(UITextView*,name,__VA_ARGS__)
//UIScrollView
#define ScrollView_(name,...) \
SY_strong_property(UIScrollView*,name,__VA_ARGS__)
//UIDatePicker
#define DatePicker_(name,...) \
SY_strong_property(UIDatePicker*,name,__VA_ARGS__)
//UIPickerView
#define PickerView_(name,...) \
SY_strong_property(UIPickerView*,name,__VA_ARGS__)
//UIWebView
#define WebView_(name,...) \
SY_strong_property(UIWebView*,name,__VA_ARGS__)
//自定义类
#define DIYObj_(class,name,...) \
SY_strong_property(class*,name,__VA_ARGS__)
/** assign */
//int
#define int_(name,...) \
SY_assign_property(int,name,__VA_ARGS__)
//float
#define float_(name,...) \
SY_assign_property(float,name,__VA_ARGS__)
//double
#define double_(name,...) \
SY_assign_property(double,name,__VA_ARGS__)
//CGFloat
#define CGFloat_(name,...) \
SY_assign_property(CGFloat,name,__VA_ARGS__)
//NSInteger
#define NSInteger_(name,...) \
SY_assign_property(NSInteger,name,__VA_ARGS__)
//NSUInteger
#define NSUInteger_(name,...) \
SY_assign_property(NSUInteger,name,__VA_ARGS__)
//BOOL
#define BOOL_(name,...) \
SY_assign_property(BOOL,name,__VA_ARGS__)
//CGRect
#define CGRect_(name,...) \
SY_assign_property(CGRect,name,__VA_ARGS__)
//CGSize
#define CGSize_(name,...) \
SY_assign_property(CGSize,name,__VA_ARGS__)
//CGPoint
#define CGPoint_(name,...) \
SY_assign_property(CGPoint,name,__VA_ARGS__)
//CGAffineTransform
#define CGAffineTransform_(name,...) \
SY_assign_property(CGAffineTransform,name,__VA_ARGS__)
//NSTimeInterval
#define NSTimeInterval_(name,...) \
SY_assign_property(NSTimeInterval,name,__VA_ARGS__)
//Class
#define Class_(name) \
SY_assign_property(Class,name)
/** weak */
//UIImageView
#define weakImageView_(name,...) \
SY_weak_property(UIImageView*,name,__VA_ARGS__)
//UIView
#define weakView_(name,...) \
SY_weak_property(UIView*,name,__VA_ARGS__)
//UILabel
#define weakLabel_(name,...) \
SY_weak_property(UILabel*,name,__VA_ARGS__)
//UIButton
#define weakButton_(name,...) \
SY_weak_property(UIButton*,name,__VA_ARGS__)
//UITableView
#define weakTableView_(name,...) \
SY_weak_property(UITableView*,name,__VA_ARGS__)
//UICollectionView
#define weakCollectionView_(name,...) \
SY_weak_property(UICollectionView*,name,__VA_ARGS__)
//UISegmentedControl
#define weakSegmentedControl_(name,...) \
SY_weak_property(UISegmentedControl*,name,__VA_ARGS__)
//UITextField
#define weakTextField_(name,...) \
SY_weak_property(UITextField*,name,__VA_ARGS__)
//UISlider
#define weakSlider_(name,...) \
SY_weak_property(UISlider*,name,__VA_ARGS__)
//UISwitch
#define weakSwitch_(name,...) \
SY_weak_property(UISwitch*,name,__VA_ARGS__)
//UIActivityIndicatorView
#define weakActivityIndicatorView_(name,...) \
SY_weak_property(UIActivityIndicatorView*,name,__VA_ARGS__)
//UIProgressView
#define weakProgressView_(name,...) \
SY_weak_property(UIProgressView*,name,__VA_ARGS__)
//UIPageControl
#define weakPageControl_(name,...) \
SY_weak_property(UIPageControl*,name,__VA_ARGS__)
//UIStepper
#define weakStepper_(name,...) \
SY_weak_property(UIStepper*,name,__VA_ARGS__)
//UITextView
#define weakTextView_(name,...) \
SY_weak_property(UITextView*,name,__VA_ARGS__)
//UIScrollView
#define weakScrollView_(name,...) \
SY_weak_property(UIScrollView*,name,__VA_ARGS__)
//UIDatePicker
#define weakDatePicker_(name,...) \
SY_weak_property(UIDatePicker*,name,__VA_ARGS__)
//UIPickerView
#define weakPickerView_(name,...) \
SY_weak_property(UIPickerView*,name,__VA_ARGS__)
//UIWebView
#define weakWebView_(name,...) \
SY_weak_property(UIWebView*,name,__VA_ARGS__)
//自定义类
#define weakDIYObj_(class,name,...) \
SY_weak_property(class*,name,__VA_ARGS__)
//delegate
#define Delegate_(class,name) \
SY_weak_property(id<class>,name)
/** base */
//copy && DIY
#define SY_copy_property(class,var,...) \
SY_set_property(class,var,copy,__VA_ARGS__)
//strong && DIY
#define SY_strong_property(class,var,...) \
SY_set_property(class,var,strong,__VA_ARGS__)
//weak && DIY
#define SY_weak_property(class,var,...) \
SY_set_property(class,var,weak,__VA_ARGS__)
//assign && DIY
#define SY_assign_property(class,var,...) \
SY_set_property(class,var,assign,__VA_ARGS__)
//DIY
#define SY_diy_property(class,var,...) \
SY_set_property(class,var,__VA_ARGS__)
//block
#define SY_set_block(class,name,...) \
SY_set_property(class,(^name)(__VA_ARGS__),copy)
//baseMacro
#define SY_set_property(class,var,...) \
@property (nonatomic, __VA_ARGS__) class var;
/**
* 其他宏
*/
//block的调用
#define Call(block,...) \
!block?:block(__VA_ARGS__);
//block的调用,并带有返回值
#define CallRerurn(block,failReturnValue,...) \
block?(block(__VA_ARGS__)):(failReturnValue)
//打印宏
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s %s:%d %s\t%s\n",__TIME__,[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__,__func__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#elif DevDebug
#define NSLog(FORMAT, ...) fprintf(stderr,"%s %s:%d %s\t%s\n",__TIME__,[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__,__func__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#elif TestDebug
#define NSLog(FORMAT, ...) fprintf(stderr,"%s %s:%d\t%s\n",__TIME__,[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#elif TestRelease
#define NSLog(FORMAT, ...) fprintf(stderr,"%s %s:%d\t%s\n",__TIME__,[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif
//初始化普通数组
#define GET_Array_(SY_name, ...) \
- (NSArray *)SY_name \
{ \
SY_lazy_(SY_name,\
NSArray *obj = [NSArray array]; \
__VA_ARGS__\
obj; \
) \
}
//初始化可变数组
#define GET_mArray_(SY_name, ...)\
- (NSMutableArray *)SY_name \
{ \
SY_lazy_(SY_name,\
NSMutableArray *obj = [NSMutableArray array]; \
__VA_ARGS__\
obj; \
) \
}
//初始化字典
#define GET_Dictionary_(SY_name, ...) \
- (NSDictionary *)SY_name \
{ \
SY_lazy_(SY_name,\
NSDictionary *obj = [NSDictionary dictionary]; \
__VA_ARGS__\
obj; \
) \
}
//初始化可变字典
#define GET_mDictionary_(SY_name, ...) \
- (NSMutableDictionary *)SY_name \
{ \
SY_lazy_(SY_name,\
NSMutableDictionary *obj = [NSMutableDictionary dictionary]; \
__VA_ARGS__\
obj; \
) \
}
//初始化自定义对象
#define GET_DIYObj_(SY_class, SY_name, ...) \
- (SY_class *)SY_name \
{ \
SY_lazy_(SY_name, \
SY_class *obj = [[SY_class alloc] init]; \
__VA_ARGS__ \
obj ; \
)\
}
#define GET_ZeroDIYObj_(SY_class, SY_name, ...) \
- (SY_class *)SY_name \
{ \
SY_lazy_(SY_name, \
__VA_ARGS__ \
obj ; \
)\
}
网友评论