1. 获取当前屏幕的宽和高:
#define KHEIGHT [UIScreen mainScreen].applicationFrame.size.height
#define KWIDTH [UIScreen mainScreen].applicationFrame.size.width
2. 可以做到系统内实现语言切换(皮肤切换等都可以用这个方法)
#define CustomLocalizedString(key, comment) \
[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:nil]
3. 识别当前的SDK版本和手机
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?YES:NO)
4. 简写系统提供的API
#define iPhoneRect CGRectMake(0.0f,0.0f,320.0f,480.0f)
#define KF(x,y,w,h) CGRectMake(x,y,w,h)
#define KImage(__name) [UIImage imageNamed:__name]
#define KSysDelegate (TestAppDelegate *)[UIApplication sharedApplication].delegate
5. 用C语言的写法,定义宏
新建一个类,在.h中定义宏:
#ifndef _UTIL_H__
#define _UTIL_H__
UIImage *bundleImage(NSString *_name);
void alert(NSString *title,NSString *info,NSString *cancelTitle);
void alertV2(NSString *title,NSString *info,NSString *cancelTitle,NSString *submitTitle);
NSString *systemTime(void);
NSString *toString(int aValue);
#endif
在.m中实现该方法(和直接写成宏不知道有什么区别)
UIImage *bundleImage(NSString *_name)
{
NSString *path = [[NSBundle mainBundle] pathForResource:_name ofType:@"png"];
UIImage *img=[UIImage imageWithContentsOfFile:path];
return img;
}
void alert(NSString *title,NSString *info,NSString *cancelTitle)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:info
delegate:nil
cancelButtonTitle:cancelTitle
otherButtonTitles:nil];
[alert show];
[alert release];
}
void alertV2(NSString *title,NSString *info,NSString *cancelTitle,NSString *submitTitle)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:info
delegate:nil
cancelButtonTitle:cancelTitle
otherButtonTitles:submitTitle,nil];
[alert show];
[alert release];
}
NSString *systemTime(void)
{
NSDate*date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd HHmmss"];
NSString *tarStr = [formatter stringFromDate:date];
[formatter release];
return tarStr;
}
NSString *toString(int aValue)
{
return [NSString stringWithFormat:@"%d",aValue];
}