iOS 通用宏定义

本文主要介绍了iOS开发中常用的宏定义,为新项目的搭建提供了一套完整的宏定义集合,包括错误处理、日志记录、颜色转换等多个方面的实用宏定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近要搭建新的项目 先把通用的宏定义给整理一下

//
//  APPUtil.h
//  Macro
//
//  Created by WY on 2018/6/15.
//  Copyright © 2018年 WY. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface APPUtil : NSObject

/**
 输出时年月日分秒毫秒
 */
+ (NSString *_Nullable)lr_stringDate;

@end
//
//  APPUtil.m
//  Macro
//
//  Created by WY on 2018/6/15.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "APPUtil.h"

@implementation APPUtil

///输出时年月日分秒毫秒
+ (NSString *)lr_stringDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss:SSS"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

@end
//
//  APPMacro.h
//  Macro
//
//  Created by WY on 2018/6/15.
//  Copyright © 2018年 WY. All rights reserved.
//
#import "APPUtil.h"

#ifndef APPMacro_h
#define APPMacro_h



#ifdef DEBUG
#define LRString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent
#define NSLog(...) printf("%s: %s 第%d行: %s\n\n",[[APPUtil lr_stringDate] UTF8String], [LRString UTF8String] ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#else
#define NSLog(...){}
#endif

/**
 获取屏幕宽高
 */
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define KScreenHeight [[UIScreen mainScreen] bounds].size.height

/**
 根据ip6的屏幕来拉伸
 */
#define kRealWidthValue(width) ((width)*(KScreenWidth/375.0f))
#define kRealHeightValue(height) ((height)*(KScreenWidth/667.0f))

/**
 强弱引用
 */
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

/**
 颜色
 */
#undef    kRGB
#define kRGB(R,G,B)    [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:1.0f]

#undef    kRGBA
#define kRGBA(R,G,B,A)    [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:A]

/*
 16进制转色值
 */
#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]
/*
 16进制转色值带透明度
 */
#define kColorFromRGBA(rgbValue, alphaValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 blue:((float)(rgbValue & 0x0000FF))/255.0 alpha:alphaValue]
/*
 随机色生成
 */
#define kRandomColorKRGBColor (arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0)

/*
 property属性快速声明
 */
#define kPropertyString(s)         @property(nonatomic,copy)NSString *s
#define kPropertyNSInteger(s)      @property(nonatomic,assign)NSInteger s
#define kPropertyFloat(s)          @property(nonatomic,assign)float s
#define kPropertyLongLong(s)       @property(nonatomic,assign)long long s
#define kPropertyNSDictionary(s)   @property(nonatomic,strong)NSDictionary *s
#define kPropertyNSArray(s)        @property(nonatomic,strong)NSArray *s
#define kPropertyNSMutableArray(s) @property(nonatomic,strong)NSMutableArray *s

/*
 字体
 */
#define kFontBold(X) [UIFont boldSystemFontOfSize:X]
#define kFont(X)  [UIFont systemFontOfSize:X]

/*
 定义UIImage对象
 */
#define kImageName(X)  [UIImage imageNamed:X]

/*
 打印当前方法名
 */
#define ITTDPRINTMETHODNAME() ITTDPRINT(@"%s",__PRETTY_FUNCTION__)

/*
 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 SINGLETON_FOR_HEADER(className)\
\
+(className *)shared##className;


#define SINGLETON_FOR_CLASS(className)\
\
+(className *)shared##className { \
static className *shared##className = nil;\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken,^{ \
shared##className =[[self alloc]init];\
});\
return shared##className;\
}


/*
 警告提示
 导入第三方库:CoreSVP
 */
#define APCoreSVPSuccess(msg) [CoreSVP showSVPWithType:CoreSVPTypeSuccess Msg:msg duration:1.5 allowEdit:NO beginBlock:nil completeBlock:nil];

#define APCoreSVPError(msg) [CoreSVP showSVPWithType:CoreSVPTypeError Msg:msg duration:1.5 allowEdit:NO beginBlock:nil completeBlock:nil];

#define APCoreSVPLoading(msg,allow) [CoreSVP showSVPWithType:CoreSVPTypeLoadingInterface Msg:msg duration:0 allowEdit:allow beginBlock:nil completeBlock:nil];

#define APCoreHiddenSVPLoading [CoreSVP dismiss];


#endif /* APPMacro_h */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值