iOS开发常见的宏定义(实用)
<div class="cnblogs_code"><pre> iOS开发过程中使用一些常用的宏可以提高开发效率,提高代码的重用性;将这些宏放到一个头文件里然后再放到工程中的-Prefix.pch文件中(或者直接放到-<span style="color: rgba(0, 0, 0, 1)">Prefix.pch中)直接可以使用,灰常方便。
做了一些分类和注释,可以根据自己习惯再添加或者删除或者修改这些宏进行使用。
#ifndef MacroDefinition_h
</span><span style="color: rgba(0, 0, 255, 1)">#define</span> MacroDefinition_h
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">AppDelegate</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> APPDELEGATE [(AppDelegate*)delegate]
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------系统设备相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取设备屏幕尺寸</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> SCREEN_WIDTH (.bounds.size.width)
<span style="color: rgba(0, 0, 255, 1)">#define</span> SCREEN_HEIGHT (.bounds.size.height)<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">应用尺寸</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> APP_WIDTH [applicationFrame].size.width
<span style="color: rgba(0, 0, 255, 1)">#define</span> APP_HEIGHT [applicationFrame].size.height
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取系统版本</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> IOS_VERSION [[ systemVersion] floatValue]
<span style="color: rgba(0, 0, 255, 1)">#define</span> CurrentSystemVersion [ systemVersion]
<span style="color: rgba(0, 0, 255, 1)">#define</span> isIOS4 ([[ systemVersion] intValue]==4)
<span style="color: rgba(0, 0, 255, 1)">#define</span> isIOS5 ([[ systemVersion] intValue]==5)
<span style="color: rgba(0, 0, 255, 1)">#define</span> isIOS6 ([[ systemVersion] intValue]==6)
<span style="color: rgba(0, 0, 255, 1)">#define</span> isAfterIOS4 ([[ systemVersion] intValue]>4)
<span style="color: rgba(0, 0, 255, 1)">#define</span> isAfterIOS5 ([[ systemVersion] intValue]>5)
<span style="color: rgba(0, 0, 255, 1)">#define</span> isAfterIOS6 ([[ systemVersion] intValue]>6)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取当前语言</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> CurrentLanguage ([ objectAtIndex:0])
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断是否 Phone 4/5/6 是否是iPad</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> Phone4 ( ? CGSizeEqualToSize(CGSizeMake(640, 960), [ currentMode].size) : NO)
<span style="color: rgba(0, 0, 255, 1)">#define</span> Phone5 ( ? CGSizeEqualToSize(CGSizeMake(640, 1136), [ currentMode].size) : NO)
<span style="color: rgba(0, 0, 255, 1)">#define</span> Phone6 ( ? CGSizeEqualToSize(CGSizeMake(750, 1334), [ currentMode].size) : NO)
<span style="color: rgba(0, 0, 255, 1)">#define</span> Phone6Plus ( ? CGSizeEqualToSize(CGSizeMake(1242,2208), [ currentMode].size) : NO)
<span style="color: rgba(0, 0, 255, 1)">#define</span> isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断是真机还是模拟器</span>
<span style="color: rgba(0, 0, 255, 1)">#if</span> TARGET_OS_IPHONE
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">iPhone Device</span>
<span style="color: rgba(0, 0, 255, 1)">#endif</span>
<span style="color: rgba(0, 0, 255, 1)">#if</span> TARGET_IPHONE_SIMULATOR
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">iPhone Simulator</span>
<span style="color: rgba(0, 0, 255, 1)">#endif</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------系统设备相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------内存相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用ARC和不使用ARC</span>
<span style="color: rgba(0, 0, 255, 1)">#if</span> __has_feature(objc_arc)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">compiling with ARC</span>
<span style="color: rgba(0, 0, 255, 1)">#else</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> compiling without ARC</span>
<span style="color: rgba(0, 0, 255, 1)">#endif</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">释放一个对象</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> SAFE_DELETE(P) if(P) { , P = nil; }
<span style="color: rgba(0, 0, 255, 1)">#define</span> SAFE_RELEASE(x) ;x=nil
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------内存相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------图片相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取本地图片</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> LOADIMAGE(file,ext) pathForResource:file ofType:ext]]
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义UIImage对象</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> IMAGE(A) pathForResource:A ofType:nil]]
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义UIImage对象</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> ImageNamed(_pointer)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可拉伸的图片</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> ResizableImage(name,top,left,bottom,right) [ resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]
<span style="color: rgba(0, 0, 255, 1)">#define</span> ResizableImageWithMode(name,top,left,bottom,right,mode) [ resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">建议使用前两种宏定义,性能高于后者
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------图片相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------颜色相关---------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> rgb颜色转换(16进制->10进制)</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> UIColorFromRGB(rgbValue)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取RGB颜色</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> RGBA(r,g,b,a)
<span style="color: rgba(0, 0, 255, 1)">#define</span> RGB(r,g,b) RGBA(r,g,b,1.0f)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">背景色</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> BACKGROUND_COLOR
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清除背景色</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> CLEARCOLOR
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------颜色相关--------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------其他----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">方正黑体简体字体定义</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> FONT(F)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">file
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取文件的文本内容,默认编码为UTF-8</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> FileString(name,ext) [ initWithContentsOfFile:[ pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]
<span style="color: rgba(0, 0, 255, 1)">#define</span> FileDictionary(name,ext) [ initWithContentsOfFile:[ pathForResource:(name) ofType:(ext)]]
<span style="color: rgba(0, 0, 255, 1)">#define</span> FileArray(name,ext) [ initWithContentsOfFile:[ pathForResource:(name) ofType:(ext)]]
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">G-C-D</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
<span style="color: rgba(0, 0, 255, 1)">#define</span> MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Alert</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> ALERT(msg) [[ initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">由角度获取弧度 有弧度获取角度</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> degreesToRadian(x) (M_PI * (x) / 180.0)
<span style="color: rgba(0, 0, 255, 1)">#define</span> radianToDegrees(radian) (radian*180.0)/(M_PI)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------其他-------------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------视图相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置需要粘贴的文字或图片</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> PasteString(string) [ setString:string];
<span style="color: rgba(0, 0, 255, 1)">#define</span> PasteImage(image) [ setImage:image];
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到视图的left top的X,Y坐标点</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> VIEW_TX(view) (view.frame.origin.x)
<span style="color: rgba(0, 0, 255, 1)">#define</span> VIEW_TY(view) (view.frame.origin.y)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到视图的right bottom的X,Y坐标点</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)
<span style="color: rgba(0, 0, 255, 1)">#define</span> VIEW_BY(view) (view.frame.origin.y + view.frame.size.height )
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到视图的尺寸:宽度、高度</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> VIEW_W(view)(view.frame.size.width)
<span style="color: rgba(0, 0, 255, 1)">#define</span> VIEW_H(view)(view.frame.size.height)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到frame的X,Y坐标点</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> FRAME_TX(frame)(frame.origin.x)
<span style="color: rgba(0, 0, 255, 1)">#define</span> FRAME_TY(frame)(frame.origin.y)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到frame的宽度、高度</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> FRAME_W(frame)(frame.size.width)
<span style="color: rgba(0, 0, 255, 1)">#define</span> FRAME_H(frame)(frame.size.height)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------------------视图相关----------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">---------------------打印日志--------------------------
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Debug模式下打印日志,当前行,函数名</span>
<span style="color: rgba(0, 0, 255, 1)">#if</span> DEBUG
<span style="color: rgba(0, 0, 255, 1)">#define</span> DLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [ UTF8String]);
<span style="color: rgba(0, 0, 255, 1)">#else</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> NSLog(FORMAT, ...) nil
<span style="color: rgba(0, 0, 255, 1)">#endif</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Debug模式下打印日志,当前行,函数名 并弹出一个警告</span>
<span style="color: rgba(0, 0, 255, 1)">#ifdef</span><span style="color: rgba(0, 0, 0, 1)"> DEBUG
# defineWDLog(fmt, ...){ UIAlertView </span>*alert = [ initWithTitle: </span><span style="color: rgba(128, 0, 0, 1)">"</span>, __PRETTY_FUNCTION__, __LINE__] message:<span style="color: rgba(0, 0, 255, 1)">delegate</span>:nil cancelButtonTitle:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">Ok</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> otherButtonTitles:nil]; ; }
</span><span style="color: rgba(0, 0, 255, 1)">#else</span><span style="color: rgba(0, 0, 0, 1)">
# define NSLog(...)
</span><span style="color: rgba(0, 0, 255, 1)">#endif</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印Frame</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> LogFrame(frame) NSLog(@"frame[X=%.1f,Y=%.1f,W=%.1f,H=%.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印Point</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> LogPoint(point) NSLog(@"Point",point.x,point.y)
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">---------------------打印日志--------------------------</span>
<span style="color: rgba(0, 0, 255, 1)">#endif</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/Free-Thinker/p/11175711.html
頁:
[1]