美文网首页
iOS项目常用宏

iOS项目常用宏

作者: 一只不靠谱的猿_ | 来源:发表于2018-04-19 10:49 被阅读29次
    #define SeverAddress                        @""
    
    
    //api服务
    #define kAPI                                [ApiService shareApiService]
    //屏幕适配因子
    #define kPERCENT(f)                         (f * [UIScreen mainScreen].bounds.size.width / 375.00)
    //普通字体
    #define FONT(f)                             [UIFont systemFontOfSize:(f)]
    //屏幕适应字体
    #define FONT_RATIO(f)                       [UIFont systemFontOfSize:(f * [UIScreen mainScreen].bounds.size.width / 375.00)]
    //屏幕宽度,高度,bounds,size
    #define WIDTH                               [UIScreen mainScreen].bounds.size.width
    #define HEIGHT                              [UIScreen mainScreen].bounds.size.height
    #define kSCREEN_BOUNDS                      [[UIScreen mainScreen] bounds]
    #define kSCREEN_BOUNDS_SIZE                 [[[UIScreen mainScreen] bounds] size]
    //view的frame因子
    #define View_FRAME_W(v)                     v.frame.size.width
    #define View_FRAME_H(v)                     v.frame.size.height
    #define View_FRAME_X(v)                     v.frame.origin.x
    #define View_FRAME_Y(v)                     v.frame.origin.y
    //RGB
    #define RGBA(r, g, b, a)                    [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
    #define HexRGB(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]
    //随机色
    #define kRandomColor                        [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
    //通用灰色线
    #define kLine_GrayColor                     [UIColor colorWithRed:231/255.f green:231/255.f blue:231/255.f alpha:0.8]
    //通用背景颜色
    #define kBackground_GrayColor               [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
    //定义UIImage对象
    #define GetImgWithName(imgName)             [UIImage imageNamed:[NSString stringWithFormat:@"%@",imgName]]
    //一些缩写
    #define kApplication                        [UIApplication sharedApplication]
    #define kUserDefaults                       [NSUserDefaults standardUserDefaults]
    #define kNotificationCenter                 [NSNotificationCenter defaultCenter]
    //App版本,ios系统版本
    #define APP_VERSION                         [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
    #define IOS_SYSTEM_VERSION                  [[[UIDevice currentDevice] systemVersion] doubleValue]
    //沙盒目录文件下获取temp,Document,Cache
    #define kPathTemp                           NSTemporaryDirectory()
    #define kPathDocument                       [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
    #define kPathCache                          [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
    //写入defaults
    #define UserDefaults_WriteObj(object, key)  [[NSUserDefaults standardUserDefaults] setObject:(object) forKey:(key)]
    //读取defaults
    #define UserDefaults_ReadObj(key)           [[NSUserDefaults standardUserDefaults] objectForKey:(key)]
    //从defaults移除
    #define UserDefaults_RemoveObjForKey(key)   [[NSUserDefaults standardUserDefaults] removeObjectForKey:(key)]
    //defaults写入磁盘
    #define UserDefaults_Synchronize            [[NSUserDefaults standardUserDefaults] synchronize]
    //获取当前语言
    #define kCurrentLanguage                    ([[NSLocale preferredLanguages] objectAtIndex:0])
    //判断是否为iPhone
    #define kisIPhone                           (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    //判断是否为iPad
    #define kisIPad                             (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    //弱引用,强引用
    #define kWeakSelf(type)                     __weak typeof(type) weak##type = type
    #define kStrongSelf(type)                   __strong typeof(type) type = weak##type
    //由角度转换弧度
    #define kDegreesToRadian(x)                 (M_PI * (x) / 180.0)
    //由弧度转换角度
    #define kRadianToDegrees(radian)            (radian * 180.0) / (M_PI)
    //获取一段时间间隔
    #define kStartTime                          CFAbsoluteTime start = CFAbsoluteTimeGetCurrent()
    #define kEndTime_Log                        NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
    //设置圆角,边框,边框颜色
    #define SetViewBorder(View, Radius, BorderWidth, BorderColor)\
    \
    [View.layer setCornerRadius:(Radius)];\
    [View.layer setMasksToBounds:YES];\
    [View.layer setBorderWidth:(Width)];\
    [View.layer setBorderColor:[Color CGColor]]
    //获取屏幕宽度与高度(考虑横屏)
    #define kScreenWidth \
    ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
    #define kScreenHeight \
    ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
    #define kScreenSize \
    ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale) : [UIScreen mainScreen].bounds.size)
    
    
    
    
    
    
    //字典取值
    //字典祛空(如果空返回@"")
    #define Dictionary(dic,key)                   (dic[key] ? dic[key] : @"")
    //获取字典的key对应的String
    #define DictionaryStringWithKey(dic,key)      [NSString stringWithFormat:@"%@",Dictionary(dic,key)]
    //获取字典的key对应的int
    #define DictionaryIntWithKey(dic,key)         [DictionaryStringWithKey(dic,key) intValue]
    //获取字典的key对应的intger
    #define DictionaryIntegerWithKey(dic,key)     [DictionaryStringWithKey(dic,key) integerValue]
    //获取字典的key对应的float
    #define DictionaryFloatWithKey(dic,key)       [DictionaryStringWithKey(dic,key) floatValue]
    //获取字典的key对应的bool
    #define DictionaryBoolWithKey(dic,key)        [DictionaryStringWithKey(dic,key) boolValue]
    //数字类型转化字符串
    #define IntegerToString(num)                  [NSString stringWithFormat:@"%ld",num]
    #define FloatIntToString(num)                 [NSString stringWithFormat:@"%.0f",num]
    #define FloatToString(num)                    [NSString stringWithFormat:@"%.2f",num]
    #define IntToString(num)                      [NSString stringWithFormat:@"%d",num]
    
    
    
    
    
    
    //字符串是否为空
    #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))
    
    
    
    
    
    
    //判断是真机还是模拟器
    #if TARGET_OS_IPHONE
    //真机
    #endif
    #if TARGET_IPHONE_SIMULATOR
    //模拟器
    #endif
    
    
    // 自定义Log日志(1)
    #if DEBUG
    #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
    
    
    // debug和release模式NSLog(2)
    //#ifdef DEBUG
    //#define NSLog(...)               NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
    //#else
    //#define NSLog(...)
    //#endif
    

    相关文章

      网友评论

          本文标题:iOS项目常用宏

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