美文网首页iOS开发基础
常用宏定义整理

常用宏定义整理

作者: SL_iOS | 来源:发表于2017-04-12 23:31 被阅读17次

    第一次准备写点东西,整理了一下项目中常用到的宏定义。


    1.获取屏幕宽度与高度

    #define SLScreenHeight [UIScreen mainScreen].bounds.size.width
    
    #define SLScreenWidth [UIScreen mainScreen].bounds.size.height
    

    2.根据rgb获得颜色

    #define SLColor(r,g,b) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:1.0]
    
    #define SLColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
    

    3.版本判断

    #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
    

    3.字号设置

    #define SLFont(F) [UIFont systemFontOfSize:(F)]
    

    4.由角度转换弧度 由弧度转换角度

    #define SLDegreesToRadian(x)  (M_PI * (x) /180.0)
    
    #define SLRadianToDegrees(radian)  (radian*180.0)/(M_PI)
    

    5.获取当前语言

    #define SLCurrentLanguage ([[NSLocale preferredLanguages]objectAtIndex:0])
    

    6.沙盒目录文件

    //获取temp
    
    #define SLPathTemp NSTemporaryDirectory()
    
    //获取沙盒 Document
    
    #define SLPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
    
    //获取沙盒 Cache
    
    #define SLPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
    

    7.判断字符串是否为空

    #define SLStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length]< 1 ? YES : NO )
    

    8.判断数组是否为空

    #define SLArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
    

    9.判断字典是否为空

    #define SLDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
    

    10.常用缩写

    #define SLApplication  [UIApplication sharedApplication]
    
    #define SLkKeyWindow  [UIApplication sharedApplication].keyWindow
    
    #define SLAppDelegate  [UIApplication sharedApplication].delegate
    
    #define SLUserDefaults  [NSUserDefaults standardUserDefaults]
    
    #define SLNotificationCenter [NSNotificationCenter defaultCenter]
    

    11.APP版本号

    #define SLAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
    

    12.获取当前语言

    #define SLCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
    

    13.判断是否为iPhone

    #define SLISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    

    14.判断是否为iPad

    #define SLISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    

    15.随机生成颜色

    #define SLRandomColor  [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

    相关文章

      网友评论

        本文标题: 常用宏定义整理

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