美文网首页
ios常用的宏

ios常用的宏

作者: 快乐的tomato | 来源:发表于2017-08-04 11:32 被阅读20次

    一、常用的颜色的宏

    /*
     宏的定义
     */
    // RGB颜色转换(16进制->10进制)
    #define UIColorFromRGB(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]
    
    // 获取RGB颜色
    #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
    #define RGB(r,g,b) RGBA(r,g,b,1.0f)
    
    // RGB  随机色
    #define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
    #define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = UIColorFromRGB(0xFCFBFB);
    //    self.view.backgroundColor = RGB(20,34,50);
        
    //    self.view.backgroundColor = randomColor;
    }
    

    二、获取屏幕的宽度

    #define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
    
    #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
    /**
     *  比例系数适配
     */
    #define m6Scale      kScreenWidth/750.0
    #define widthradio   [UIScreen mainScreen].bounds.size.width  / 375.0
    #define heightradio   [UIScreen mainScreen].bounds.size.height / 667.0
    

    三、打印

    /**
     *  如果不需要log,把1改成0
     */
    
    #define  myTest  1
    #if myTest
    
    #define NSLog(FORMAT, ...) fprintf(stderr,"[%s:%d行] %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
    
    #else
    
    #define NSLog(FORMAT, ...) nil
    #endif
    
    ============================或者
    //项目开发中,我们会在许多地方加上Log,但是发布的时候又不想用这些Log,我们也不可能一个一个的删除,所以自定义Log是必然的!
    
    #ifdef DEBUG
    #define LRLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
    
    #else
    
    #define LRLog(...)
    
    #endif
    

    四、判断设备的系统版本和机型

    /**
     监测系统版本
     */
    #define iOS8 ([[UIDevice currentDevice].systemVersion intValue]>=8?YES:NO)
    #define iOS9 ([[UIDevice currentDevice].systemVersion intValue]>=9?YES:NO)
    #define iOS10 ([[UIDevice currentDevice].systemVersion intValue]>=10?YES:NO)
    #define iOS11Later ([UIDevice currentDevice].systemVersion.floatValue >=11.0f)
    #define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
    /**
     监测具体机型
     */
    //导入头文件
    #import "sys/utsname.h"
    
    
    - (NSString *)getCurrentDeviceModel
    {
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        //iPhone
        if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (A1428)";
        if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (A1429/A1442)";
        if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (A1456/A1532)";
        if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (A1507/A1516/A1526/A1529)";
        if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (A1453/A1533)";
        if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (A1457/A1518/A1528/A1530)";
        if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6 (A1549/A1586)";
        if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus (A1522/A1524)";
        if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s (A1633/A1688/A1691/A1700)";
        if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus (A1634/A1687/A1690/A1699)";
        if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
        if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
        
        return platform;
    }
    

    参考:https://www.theiphonewiki.com/wiki/Models ,这个是苹果官方的设备类型说明,可以参考一下
    五、NSUserDefault

    //存储到本地
    #define UserDefaults(Object, Key)\
    [[NSUserDefaults standardUserDefaults] setObject:Object forKey:Key];\
    
    
    #define defaults [NSUserDefaults standardUserDefaults]
    

    相关文章

      网友评论

          本文标题:ios常用的宏

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