可以提升开发速度的宏

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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值