iOS/objective-Cのマクロ設定のまとめ
必要に応じてdefine.h
にマクロを追記していき、次のプロジェクトではそのままマクロファイルを受け継ぐようにしていると、だんだんマクロ設定が増えてきたので一旦まとめてみようかと思います。
画面サイズ
//スクリーンの高さ #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) //スクリーンの幅 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) /** 使用例 label.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); */
バージョン系
//アプリのバージョン #define APP_VERSION [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] //ビルドバージョン #define BUILD_VERSION [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"] //OSのバージョン #define SYSTEM_VERSION ([[UIDevice currentDevice] systemVersion]) //OSバージョンによる処理の振り分け #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) /** 使用例 if (SYSTEM_VERSION_LESS_THAN(@"8.0")) { NSLog(@"8.0未満"); }else{ NSLog(@"8.0以上"); } */
カラー系
#define RGBA(r, g, b, a) [UIColor colorWithRed: (r)/255.0 green: (g)/255.0 blue: (b)/255.0 alpha: (a)] #define RGB(r, g, b) [UIColor colorWithRed: (r)/255.0 green: (g)/255.0 blue: (b)/255.0 alpha:1.0] //alpha=1固定の場合 /** 使用例 label.textColor = RGBA(240.0, 129.0, 163.0, 1.0); このようにRGBとalpha値を数値で指定 */ #define RGBA_HEX(rgbHex,a) [UIColor colorWithRed:((float)((rgbHex & 0xFF0000) >> 16))/255.0 green:((float)((rgbHex & 0xFF00) >> 8))/255.0 blue:((float)(rgbHex & 0xFF))/255.0 alpha:a] #define RGB_HEX(rgbHex) [UIColor colorWithRed:((float)((rgbHex & 0xFF0000) >> 16))/255.0 green:((float)((rgbHex & 0xFF00) >> 8))/255.0 blue:((float)(rgbHex & 0xFF))/255.0 alpha:1.0] //alpha=1固定の場合 /** 使用例 label.textColor = RGBA_HEX(0x00bfff,1.0); 色は頭に0xをつけて16進の値を設定、alpha値は数値で0.0〜1.0を指定 デザイナーは16進数で指定してくることが多いですね。 */ #define MyAppColorPink RGBA_HEX(0xff69b4,1.0); /** 使用例 label.textColor = MyAppColorPink; アプリ毎に色を定義して名前をつけるます。 このかたちで定義しておくと入力時に補完もされるのでかなり楽になります。 */
言語取得
#define LANGUAGE ([NSLocale preferredLanguages][0])
retina判定
#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [UIScreen mainScreen].scale > 1)
AppDelegate
#define AD ((AppDelegate*)[[UIApplication sharedApplication] delegate]) /** 使用例 AppDelegate.hでプロパティ宣言 @property(nonatomic, strong)NSDictionary* infoDict; 任意のソースファイルで #import AppDelegate.h としておけば AD.infodictでアプリ内のどこからでも参照可能 */
通知系
//通知を受ける #define receiveNotification(receiveName,receiveSelector) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSelector) name:receiveName object:nil]; //通知を送る #define sendNotification(sendName,sendObject) [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:sendName object:sendObject]]; //通知の監視を解除 #define removeNotification(removeNotificationName) [[NSNotificationCenter defaultCenter] removeObserver:self name:removeNotificationName object:nil]; /** 使用例 //受け側 //通知の設定 receiveNotification(@"notifiTest",test); 通知の名前と叩くメソッド名を設定 //通知の解除 removeNotification(@"notifiTest"); viewを閉じるタイミングなどで実行 通知の名前を設定 //通知受信時の処理 -(void)test:(NSDictionary*)object{ NSDictionary* dict = object.object; //任意の処理 } //付加するオブジェクトがnilの場合は -(void)test{ //任意の処理〜 } //通知側 sendNotification(@"notifiTest",infoDict); 通知の名前に続いて付加するオブジェクト(nilでも可)を設定 通知の名前を'#define'で設定するとさらに使い勝手が良くなりますね #define NTFnotifiTest @"notifiTest" */