iOS常用宏(不断更新)

  • #define后面一般不加;
    • 宏里面用#argc,是要取argc的值
按比例拉伸??在iPhone6尺寸应该是什么样的高度!
#define DIMENSION_SELF_ADAPT(width, height)\
(width > 0 ? height * kScreenWidth / width : 0)

#define DIMENSION_SELF_ADAPT_375(height) DIMENSION_SELF_ADAPT(375, height)


  1. 宏定义
#ifndef MGMacro_h
#define MGMacro_h
……
#endif /* MGMacro_h */
  1. 颜色宏
/************ 随机颜色 ************/
#define kRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0]
// rgb颜色转换(十六进制)
#define kColorWithHex(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 kColorWithRGB(R,G,B)  [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
#define kColorWithRGBA(R,G,B,A)  [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]


  • 尺寸宏
/************ 屏幕尺寸 ************/
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define STATUS_HEIGHT [[UIApplication sharedApplication] statusBarFrame].size.height
#define StatusBar_HEIGHT 20
#define NavigationBar_HEIGHT 44
#define NavigationBarIcon 20
#define TabBar_HEIGHT 49
#define TabBarIcon 30
// 获取view的frame属性
#define GetViewWidth(view)  view.frame.size.width
#define GetViewHeight(view)  view.frame.size.height
#define GetViewX(view)      view.frame.origin.x
#define GetViewY(view)      view.frame.origin.y
  • 工具宏
/************ 角度转弧度 ************/
#define kDegrees2Radian(x)  (M_PI * (x) / 180.0)
//弧度转角度
#define kRadian2Degrees(radian)  (radian * 180.0) / (M_PI)
// 去掉首尾空格
#define TRIM(tempStr) [tempStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
// 获取图片资源
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
// 读取本地图片
#define kLoadImage(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]


  • 打印宏
/************ 自定义NSLog ************/
#ifdef DEBUG
#define MGLog(...) NSLog(__VA_ARGS__)
#else
#define MGLog(...)
#endif
// 打印方法名
#define MGLogFunc MGLog(@"%s",__FUNCTION__);
  • 设备宏
/************ APP版本号 ************/
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
// APP名称
#define kAppName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]
// 获取当前语言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
// 判断是否为iPhone
#define kISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
// 判断是否为iPad
#define kISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// 系统版本号
#define kSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]
// 判断是否为iOS8
#define iOS8 ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)
// 根据尺寸判断 是否为4inch
#define is4Inch ([UIScreen mainScreen].bounds.size.height == 568)


  • 文件宏
/************ 获取沙盒Document路径 ************/
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
// 获取沙盒temp路径
#define kTempPath NSTemporaryDirectory()
// 获取沙盒Cache路径
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
  • 系统宏
/************ 系统application ************/ 
#define kApplication        [UIApplication sharedApplication]
#define kKeyWindow          [UIApplication sharedApplication].keyWindow
#define kAppDelegate        [UIApplication sharedApplication].delegate
#define kUserDefaults       [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]


  • 判断宏
/************ 判断 ARC 和 MRC ************/ 
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
/************ 判断 真机 和 模拟器 ************/ 
#if TARGET_OS_IPHONE
// iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
// iPhone Simulator
#endif
  • 弱引用/强引用
#define kWeakSelf(type) __weak typeof(type) weak##type = type;
#define kStrongSelf(type) __strong typeof(type) type = weak##type;
  • 适配屏幕尺寸宽高比
#define KWidth(width) (IS_SCREEN_4_INCH ? (width * (CGFloat)320 / 375):(IS_SCREEN_47_INCH ? (width):(width *(CGFloat)414 / 375)))
#define KHeight(height)  (IS_SCREEN_4_INCH ? (height * (CGFloat)568 / 667):(IS_SCREEN_47_INCH ? (height):(height *(CGFloat)736 / 667)))
IS_SCREEN_4_INCH


  • 判断系统版本代码
// 判断系统版本
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

+ (UIDevice *)currentDevice;  
@property(nonatomic,readonly,retain) NSString    *name;              // e.g. "My iPhone"
@property(nonatomic,readonly,retain) NSString    *model;             // e.g. @"iPhone", @"iPod touch"
@property(nonatomic,readonly,retain) NSString    *localizedModel;    // localized version of model
@property(nonatomic,readonly,retain) NSString    *systemName;        // e.g. @"iOS"
@property(nonatomic,readonly,retain) NSString    *systemVersion;     // e.g. @"4.0"
@property(nonatomic,readonly) UIDeviceOrientation orientation;       // return current device orientation.  this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.
@property(nonatomic,readonly,retain) NSUUID      *identifierForVendor NS_AVAILABLE_IOS(6_0);      // a UUID that may be used to uniquely identify the device, same across apps from a single vendor.

待修改!!!

  • 网络宏
#define APIURL  @"http://xxxxx/"
#define APILogin  [APIURL stringByAppendingString:@"Login"]
// 账号相关信息
#define kAppKey @"3637170628"
#define kAppSecret @"b4990ef2e737298552a1c8388fca78c3"
#define kRedirectURI @"https://api.weibo.com/oauth2/default.html" 
  • 其他宏
// 方正黑体简体字体定义
#define FONT(size) [UIFont fontWithName:@"FZHTJW--GB1-0" size:size]
//获取一段时间间隔
#define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define kEndTime   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

颜色相关知识

  • 24-bit位颜色,每一位占8道,3个颜色通道

白色 #ffffff 最不纯净的颜色
黑色 #000000 是最纯净的颜色
红色 #ff0000
黄色 #ffff00


参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 定义了一些常用的宏,写代码的时候用起来挺方便的,添加了pch文件,设置了相对路径;设置pch文件相对路径的方法:设...
    SnailLi阅读 1,592评论 0 1
  • iOS开发过程中,使用的一些常用宏定义 字符串是否为空#define kStringIsEmpty(str) ([...
    goyohol阅读 5,312评论 30 85
  • #ifndef iOS_Constants_h #define iOS_Constants_h /* ******...
    莫离_焱阅读 252评论 0 1
  • /**获取屏幕宽度与高度 导航,tabbar高度*/ #define SCREEN_WIDTH [UIScree...
    MUYO_echo阅读 657评论 0 3
  • 来源于CocoaChina 在工作中, 很多小伙伴都会在PCH文件定义一些常用的宏,但是又怕写这些简单的宏浪费时间...
    iOS学末阅读 770评论 3 7