美文网首页iOS技术iOS大咖iOS开发常用知识点
我在iOS项目中常用的宏,提高不少效率,持续更新。。。

我在iOS项目中常用的宏,提高不少效率,持续更新。。。

作者: 翼须付出 | 来源:发表于2016-09-19 15:41 被阅读402次

    在项目中合理的使用一些宏,可以很大的提高代码效率,代码简洁度!下面我就列举一些我常用的宏,随着项目的进行,我会持续更新!

    在这里呢,我一般是把常用宏分类写:基础宏,颜色宏,地址宏,通知文字等常量

    • 基础宏
    /** MainScreen bounds */ 
    #define Main_Screen_Bounds [[UIScreen mainScreen] bounds]
    /** 屏幕宽高 */
    #define kLFSCREEN_W [UIScreen mainScreen].bounds.size.width
    #define kLFSCREEN_H [UIScreen mainScreen].bounds.size.height
    /** 导航栏的高度 */
    #define VIEW_TOP_HEIGHT (IOS7_OR_LATER ? 64.0f : 44.0f)  
    /** 状态栏的size */
    #define kstatusBarSize [[UIApplication sharedApplication] statusBarFrame].size
    
    /** 屏幕尺寸 */
    #define IS_SCREEN_4_INCH    ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
    #define IS_SCREEN_35_INCH   ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
    #define IS_SCREEN_47_INCH   ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
    #define IS_SCREEN_55_INCH   ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
    
    /** 屏幕尺寸 */
    #define is_screen_35_inch   CGSizeEqualToSize([UIScreen mainScreen].bounds.size, CGSizeMake(320, 480))
    #define is_screen_40_inch   CGSizeEqualToSize([UIScreen mainScreen].bounds.size, CGSizeMake(320, 568))
    #define is_screen_47_inch   CGSizeEqualToSize([UIScreen mainScreen].bounds.size, CGSizeMake(375, 667))
    #define is_screen_55_inch   CGSizeEqualToSize([UIScreen mainScreen].bounds.size, CGSizeMake(414, 736))
    
    //不同屏幕尺寸字体适配(320,568是因为效果图为IPHONE5 如果不是则根据实际情况修改)
    #define kScreenWidthRatio  (Main_Screen_Width / 320.0)
    #define kScreenHeightRatio (Main_Screen_Height / 568.0)
    #define AdaptedWidth(x)  ceilf((x) * kScreenWidthRatio)
    #define AdaptedHeight(x) ceilf((x) * kScreenHeightRatio)
    #define AdaptedFontSize(R)     CHINESE_SYSTEM(AdaptedWidth(R))
    
    // 字体大小(常规/粗体)
    #define BOLDSYSTEMFONT(FONTSIZE)[UIFont boldSystemFontOfSize:FONTSIZE]
    #define SYSTEMFONT(FONTSIZE)    [UIFont systemFontOfSize:FONTSIZE]
    #define FONT(NAME, FONTSIZE)    [UIFont fontWithName:(NAME) size:(FONTSIZE)]
    
    //中文字体
    #define CHINESE_FONT_NAME  @"Heiti SC"
    #define CHINESE_SYSTEM(x) [UIFont fontWithName:CHINESE_FONT_NAME size:x]
    
    //重写NSLog,Debug模式下打印日志和当前行数
    #if DEBUG
    
    #define RZLog(FORMAT, ...) do {                                     \
    fprintf(stderr,"\nfunction:%s \nline:%d content: -> %s\n",          \
    __FUNCTION__,                                                       \
    __LINE__,                                                           \
    [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);    \
    } while (0)
    
    #else
    #define RZLog(FORMAT, ...) nil
    #endif
    

    /** 系统版本 */
    #define FSystemVersion  ([[[UIDevice currentDevice] systemVersion] floatValue])
        #define DSystemVersion  ([[[UIDevice currentDevice] systemVersion] doubleValue])
    #define SSystemVersion  ([[UIDevice currentDevice] systemVersion])
    
    #define ios7_or_later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0)
    #define ios8_or_later ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
    #define ios9_or_later ([UIDevice currentDevice].systemVersion.floatValue >= 9.0)
    
    /** 当前app信息 */
    #define kbundelId [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]
    #define kexecutable  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]
    #define kdisplayName  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
    
    /** 应用的Verison号 */
    #define kappVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
    
    /** build号 */
    #define kappBuild [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
    
    /** AppDelegate对象 */
    #define AppDelegateInstance [[UIApplication sharedApplication] delegate]
    

    //获取图片资源
    #define GetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
    
    //Library/Caches 文件路径
    #define FilePath ([[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil])
    //获取沙盒Document路径
    #define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
    //获取沙盒temp路径
    #define kTempPath NSTemporaryDirectory()
    //获取沙盒Cache路径
    #define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
    
    //判断是真机还是模拟器
    #if TARGET_OS_IPHONE
    //真机
    #endif
    
    #if TARGET_IPHONE_SIMULATOR
    //模拟器
    #endif
    
    //NSUserDefaults 实例化
    /**  以key,value存储信息 */
    #define USERDEFAULTSET(key,value) [[NSUserDefaults standardUserDefaults] setObject:value forKey:key]
    /** 以key取出value */
    #define USERDEFAULTS(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]
    /** 以key删除value */
    #define USERDEFAULTREMOVE(key) [[NSUserDefaults standardUserDefaults] removeObjectForKey:key]
    /** 立即同步 */
    #define USERDEFAULTSYNCHRONIZE  [[NSUserDefaults standardUserDefaults] synchronize]
    
    /** 是否是iPad */
    #define isPad  (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    /** 判断是否为iPhone */
    #define isIPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    
    • 颜色宏
    /** 三原色 */
    #define UIColorRGBA(_r, _g, _b, _a) [UIColor colorWithRed:_r/255.f green:_g/255.f blue:_b/255.f alpha:_a]
    #define UIColorRgb(_r, _g, _b) UIColorRGBA(_r, _g, _b, 1.0f)
    
    /** 传入色值 */
    #define LFHEXCOLOR_a(hex,a) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16)) / 255.0 green:((float)((hex & 0xFF00) >> 8)) / 255.0 blue:((float)(hex & 0xFF)) / 255.0 alpha:(a)]
    #define LFHEXCOLOR(hex) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16)) / 255.0 green:((float)((hex & 0xFF00) >> 8)) / 255.0 blue:((float)(hex & 0xFF)) / 255.0 alpha:1]
    
    /** 随机色  */
    #define LFRandomColor UIColorRgb(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
    
    /** 背景色 */
    #define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
    
    /** 透明背景色 */
    #define CLEARCOLOR [UIColor clearColor]
    
    /*! navi蓝色 */
    #define RZ_NaviBgBlueColor     RZ_COLOR(92, 177, 251, 1.0)
    #define RZ_BGGrayColor         RZ_COLOR(239, 239, 239, 1.0)
    #define RZ_TEXTGrayColor       RZ_COLOR(148, 147, 133, 1.0)
    #define RZ_BLUEColor           RZ_COLOR(78, 164, 255, 1.0)
    #define RZ_BGClearColor        [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.7f]
    
    /*! 主题浅绿 */
    #define RZ_Them_greenColor     RZ_COLOR(30, 198, 181, 1.0)
    
    /*! 白色 */
    #define RZ_White_Color         [UIColor whiteColor]
    
    /*! 红色 */
    #define RZ_Red_Color           [UIColor redColor]
    
    /*! 黄色 */
    #define RZ_Yellow_Color        [UIColor yellowColor]
    
    /*! 绿色 */
    #define RZ_Green_Color         [UIColor greenColor]
    
    /*! 蓝色 */
    #define RZ_Blue_Color          [UIColor blueColor]
    
    /*! 无色 */
    #define RZ_Clear_Color         [UIColor clearColor]
    
    /*! 橙色 */
    #define RZ_Orange_Color        [UIColor orangeColor]
    
    /*! 黑色 */
    #define RZ_Black_Color         [UIColor blackColor]
    
    /*! 浅灰色色 */
    #define RZ_LightGray_Color     [UIColor lightGrayColor]
    

    • 常量,地址,三方key都可以写成宏
    /*! 常量 */
    static NSString* const kBase_URL=@"www.baidu.com";
    
    • iOS 11 适配
    #define kAdjustmentBehavior(VC, view) if (@available(iOS 11.0, *)) {                \
        view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;  \
    } else {                                                                            \
        VC.automaticallyAdjustsScrollViewInsets = NO;                                   \
    }                                                                                   \
    
    • 线程队列
    /*! 主线程同步队列 */
    #define dispatch_main_sync_safe(block)          \
    if ([NSThread isMainThread]) {                  \
    block();                                        \
    } else {                                        \
    dispatch_sync(dispatch_get_main_queue(), block);\
    }
    /*! 主线程异步队列 */
    #define dispatch_main_async_safe(block)        \
    if ([NSThread isMainThread]) {                 \
    block();                                       \
    } else {                                       \
    dispatch_async(dispatch_get_main_queue(), block);\
    }
    

    以下是引用的一些宏

    //字符串是否为空
    #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))
    
    //一些缩写
    #define kApplication [UIApplication sharedApplication]
    #define kKeyWindow [UIApplication sharedApplication].keyWindow
    #define kAppDelegate [UIApplication sharedApplication].delegate
    #define kUserDefaults [NSUserDefaults standardUserDefaults]
    #define kNotificationCenter [NSNotificationCenter defaultCenter]
    
    /** 当前语言 */
    #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
    
    //获取一段时间间隔
    #define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    #define kEndTime   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
    
    //弱引用/强引用
    #define kWeakSelf(type)   __weak typeof(type) weak##type = type;
    #define kStrongSelf(type) __strong typeof(type) type = weak##type;
    
    /*! 复制文字内容 */
    #define BAKit_CopyContent(content) [[UIPasteboard generalPasteboard] setString:content]
    
    //上传图片相关
    #define kImageCollectionCell_Width floorf((Main_Screen_Width - 10*2- 10*3)/3)
    //最大的上传图片张数
    #define kupdateMaximumNumberOfImage 12
    
    //由角度转换弧度
    #define kDegreesToRadian(x)      (M_PI * (x) / 180.0)
    //由弧度转换角度
    #define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)

    相关文章

      网友评论

      • 熊猫丶Panda:写这么多宏定义好吗?
        翼须付出:@熊猫丶Panda 使用宏定义过多确实不太好,主要是影响程序可读性,容易出错,对带参数的宏来说,缺乏对参数的类型检测,有很大的隐患,所以要合理使用宏定义
        熊猫丶Panda:@翼须付出 宏定义多了不好吧
        翼须付出:@Master丶king 你要是项目中都用的上,没什么问题
      • LovelyYilia:厉害
        翼须付出:@LovelyYilia 基本都是最近几个月的项目中用的!

      本文标题:我在iOS项目中常用的宏,提高不少效率,持续更新。。。

      本文链接:https://www.haomeiwen.com/subject/hfhnettx.html