美文网首页
2020-07-16 OC项目中自己常用的宏定义

2020-07-16 OC项目中自己常用的宏定义

作者: Vinch | 来源:发表于2020-07-16 15:53 被阅读0次

    ifndef UtilsMacro_h

    define UtilsMacro_h

    endif /* UtilsMacro_h */

    /************************Version****************************/

    define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

    define kAppBuild [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

    /************************弱引用/强引用****************************/

    define WEAKSELF typeof(self) __weak weakSelf=self;

    /************************翻页****************************/

    define kPage 1

    define kPageSize 20

    /************************打印****************************/

    ifdef DEBUG

    define kNSLog(...) NSLog(@"\n打印结果:\n %s 第%d行 \n %@\n\n",func,LINE,[NSString stringWithFormat:VA_ARGS])

    else

    define kNSLog(...)

    endif

    define kLogFunc NSLog(@"方法名:\n %s", func);//只打印函数名称

    define kLogResponseObject kNSLog(@"%@",responseObject);

    define kLogError kNSLog(@"%@",error);

    define kNSHomeDirectory kNSLog(@"%@",NSHomeDirectory());

    /************************颜色****************************/
    // 颜色RGB转换(16进制->10进制)

    define kColorFromRGB(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 kRGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

    define kRGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

    define kHexColor(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:1.0]

    define kHexColorA(c,a) [[UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:1.0] colorWithAlphaComponent:a]

    define kClearColor [UIColor clearColor]

    define kUsuallyColor kHexColor(0x679ffe)

    define kSelectedTabbarColor kHexColor(0x5661fa)

    define kUnSelectTabbarColor kHexColor(0xcfcfcf)

    define kBlackColor kHexColor(0x333333)

    define kDarkGrayColor kHexColor(0x555555)

    define kDeepGrayColor kHexColor(0x666666)

    define kPurpleColor kHexColor(0x9795F4)

    define kBlueColor kHexColor(0x679FFE)

    define kLightBlueColor kHexColor(0x6a7dff)

    define kBgColor kHexColor(0xf6f7f8)

    define kRedColor kHexColor(0xfd4837)

    define kGrayColor kHexColor(0xcfcfcf)

    define kLightGrayColor kHexColor(0xf6f7f8)

    define kAssistGrayColor kHexColor(0xdedede)

    define kWhiteColor kHexColor(0xffffff)

    define kLineColor kHexColor(0xebedf0)

    define kGrayLightColor kHexColor(0x96a0ac)

    define KColorType(a) [UIColor colorWithType:a]

    /************************字体****************************/

    define kFont(float) [UIFont systemFontOfSize:float]

    define kBoldFont(float) [UIFont boldSystemFontOfSize:float]

    /************************尺寸 + 版本****************************/
    /*
    iPhone X尺寸问题
    分辨率11252436 (375812)
    导航栏为 44+44 原来为20+44
    底部tabbar 49+34 其余屏幕 49+0
    */

    define kiOS11 @available(iOS 11.0, *)

    //低于某个系统版本

    define iOS10_AndEarlier ([[[UIDevice currentDevice]systemVersion]floatValue] < 11.0)

    define iOS9_AndEarlier ([[[UIDevice currentDevice]systemVersion]floatValue] < 10.0)

    //高于某个系统版本

    define kPhoneVersion [[UIDevice currentDevice] systemVersion]

    define iOS10_AndLater ([[[UIDevice currentDevice]systemVersion]floatValue] >= 10.0)

    define iOS9_AndLater ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0)

    define iOS8_AndLater ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)

    //判断是否是ipad

    define isPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)

    //判断iPhone4系列

    define kiPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)

    //判断iPhone5系列

    define kiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)

    //判断iPhone6 6s 7系列

    define kiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)

    //判断iPhone6p 6sp 7p系列

    define kiPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)

    //判断iPhoneX,Xs(iPhoneX,iPhoneXs)

    define IS_IPHONE_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)

    //判断iPhoneXr

    define IS_IPHONE_Xr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)

    //判断iPhoneXsMax

    define IS_IPHONE_Xs_Max ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size)&& !isPad : NO)

    //判断iPhoneX所有系列

    define IS_PhoneXAll (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs_Max)

    //TODO:还需适配xr、xs Max

    define kiPhoneX ([UIScreen mainScreen].bounds.size.width == 375.0f && [UIScreen mainScreen].bounds.size.height == 812.0f)

    // 尺寸

    define kScreenWidth [UIScreen mainScreen].bounds.size.width

    define kScreenHeight [UIScreen mainScreen].bounds.size.height

    define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height//状态栏高度

    define kNavigationBarHeight self.navigationController.navigationBar.frame.size.height//导航栏高度

    define KTopHeight (kStatusBarHeight + kNavigationBarHeight)//状态栏+导航栏高度

    define kTabBarHeight (IS_PhoneXAll ? 83.f : 49.f)//tabbar高度

    define kScreenBounds [UIScreen mainScreen].bounds

    define kTableViewCellHeight 56

    define kIphoneXButtomHeight 34.f//底部高度

    define KSafeBottom (IS_PhoneXAll ? 34 :0) //

    define KViewHeightDefualt (kScreenHeight-KTopHeight-KSafeBottom)

    // 设备当前横竖状态

    define kDeviceOrientation [[UIDevice currentDevice]orientation]

    /************************适配****************************/

    // 现在产品设计稿有以iPhone6s为基准的

    define kScreenWidthRatio (kScreenWidth / 375.0)

    //TODO:高度适配

    define kScreenHeightRatio (kScreenWidth / 375.0)//(kScreenHeight / 667.0)-适配ihopneX以屏幕宽度比例为准

    define kAdaptedWidth(x) (ceilf((x) * kScreenWidthRatio))

    define kAdaptedHeight(x) (ceilf((x) * kScreenHeightRatio))

    define kMarginWidth kAdaptedWidth(8)

    define kHeight kAdaptedHeight(8)

    define kLineViewHeight 0.5

    // 音视频缩小浮窗

    define VHFloatingViewSPWidth 75.0

    define VHFloatingViewSPHeight 135.0

    define VHFloatingViewYPWidth 56.0

    define VHFloatingViewYPHeight 72.0

    /************************缩写****************************/
    // 获取图片资源(本地)

    define kImageName(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

    define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

    define kString(object) [NSString stringWithFormat:@"%@",object]

    define kStrcat(string, args...) [NSString stringWithFormat:string, args]

    define kGetImageURL(url) [NSURL URLWithString:getCompleteUrl(url)]

    define KImageDefualt [UIImage imageNamed:@"ic_default"]

    define KImageUserDefualt [UIImage imageNamed:@"ic_default_avatar"]

    // View 圆角和加边框

    define KViewBorderRadius(View, Radius, Width, Color)\


    [View.layer setCornerRadius:(Radius)];
    [View.layer setMasksToBounds:YES];
    [View.layer setBorderWidth:(Width)];
    [View.layer setBorderColor:[Color CGColor]]

    //View 圆角

    define KViewRadius(View, Radius)\


    [View.layer setCornerRadius:(Radius)];
    [View.layer setMasksToBounds:YES]

    // 系统一些常用缩写

    define kApplication [UIApplication sharedApplication]

    //#define kKeyWindow [UIApplication sharedApplication].keyWindow//(ios13 之后将会被弃用)

    define kKeyWindow [[[UIApplication sharedApplication] windows] objectAtIndex:0]

    define kAppDelegate [UIApplication sharedApplication].delegate

    define kUserDefaults [NSUserDefaults standardUserDefaults]

    define kSetUserDefaults(value,key) [kUserDefaults setObject:value forKey:key];\

                                      [kUserDefaults synchronize];
    

    define kGetUserDefaults(key) [kUserDefaults objectForKey:key]

    define kRemoveUserDefaults(key) [kUserDefaults removeObjectForKey:key]

    define kNotificationCenter [NSNotificationCenter defaultCenter]

    //#define kNavigationView self.navigationController.view

    /*************************判断字符串、数组、字典、对象为空****************************/
    //字符串是否为空

    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) ? YES : NO)

    //字典是否为空

    define kDictIsEmpty(dic) ((dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0) ? YES : NO)

    //是否是空对象

    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)) ? YES : NO)

    /*************************获取一些路径****************************/
    //获取沙盒Document路径

    define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

    //获取沙盒temp路径

    define kTempPath NSTemporaryDirectory()

    //获取沙盒Cache路径

    define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

    //Library/Caches 文件路径

    define kFilePath ([[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil])

    ///默认存放路径

    define kBomupPath kStrcat(@"%@/bomup", kDocumentPath)

    define kBomupFilePath kStrcat(@"%@/File", kDocumentPath)

    // 防止多次调用

    define VHHalfSeconds 0.5//半秒

    define kPreventRepeatClickTime(seconds) \

    static BOOL shouldPrevent;
    if (shouldPrevent) return;
    shouldPrevent = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((seconds) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    shouldPrevent = NO;
    }); \

    //多屏互动时聊天历史中的禁止点击事件

    define VHISDP [kGetUserDefaults(ACTION_TYPING_DUOPING_ING) isEqualToString:@"1"]

    define kStopClickWhenDP() \

    if ([kGetUserDefaults(ACTION_TYPING_DUOPING_ING) isEqualToString:@"1"]) return; \

    //判断字符

    define NUM @"0123456789"

    define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

    define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

    相关文章

      网友评论

          本文标题:2020-07-16 OC项目中自己常用的宏定义

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