iOS 宏定义
2017-05-23 本文已影响8人
ZIM东东
debug时打印,下面一句是解决xcode8打印不全的问题;
#ifdef DEBUG
#define DLLog(...) NSLog(__VA_ARGS__)
#else
#define DLLog(...)
#endif
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
屏幕宽高,或者比例
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define kw(R) (R) * (KScreenWidth) / 320.0
#define kh(R) (R) * (KScreenHeight) / 568.0
NSUserDefaults 存储用户信息:
//为NSUserDefaults 添加object和key
#define Kusersave(object,key) [[NSUserDefaults standardUserDefaults] setObject:object forKey:key]
//读取NSUserDefaults中key的元素
#define Kuserread(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]
//删除NSUserDefaults中key的元素
#define Kuserdelete(key) [[NSUserDefaults standardUserDefaults] removeObjectForKey:key]
获取沙盒路径:
//获取temp
#define kPathTemp NSTemporaryDirectory()
//获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
字符串,数组,字典,判空:
//字符串是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
//数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
//字典是否为空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
//是否是空对象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
设置 view 圆角和边框
#define KViewBorderRadius(View, Radius, Width, Color)\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
设置 lable 常用属性
#define kLabSetup(lab,textcolor,textfont,backgroundcolor,ttAlignment)\
lab.textColor = textcolor;\
lab.font = textfont;\
lab.textAlignment = ttAlignment;\
lab.backgroundColor = backgroundcolor;
设置 button 常用属性
#define kButtonSetup(btu,title,textcolor,textfont,backcolor,imgstr)\
[btu setTitle:title forState:UIControlStateNormal];\
[btu setTitleColor:textcolor forState:UIControlStateNormal];\
btu.titleLabel.font = textfont;\
[btu setBackgroundColor:backcolor];\
[btu setImage:[UIImage imageNamed:imgstr] forState:UIControlStateNormal];
设置 textfield 常用属性
#define kTextFieldSetup(textField,clearmode,keyboard,phtext,textcolor,textfont,backgroundcolor)\
textField.clearButtonMode=clearmode;\
textField.keyboardType=keyboard;\
textField.placeholder=phtext;\
textField.textColor=textcolor;\
textField.font=textfont;\
textField.backgroundColor=backgroundcolor;
//加载较大的png图片
# define Kimage(imageName) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:imageName ofType:@"png"]]
//转换字符串
#define KCHANGE_STR(string) [[NSString alloc]initWithFormat:@"%@",string]