LoginSignup
31
32

More than 5 years have passed since last update.

iPhoneアプリ開発で、フォントや色をスッキリ管理するやり方

Posted at

デザイナーの方とアプリを作っていると、文字などで使う色のパターンがある程度決まってくる。
それを、いちいちxibやstoryboardで指定するのは面倒だし、コードで指定する場合でも毎回[UIColor colorWithRed:green:blue]で指定していると変更があった時に死ぬ。
そこで、ちょっとしたカテゴリを作成しておくとスッキリ管理できて簡単に使えるようになる。

UIColor+MYExtention.h
#import <UIKit/UIKit.h>

@interface UIColor (MYExtention)
+ (UIColor *)myC1;
+ (UIColor *)myC2;
@end
UIColor+MYExtention.m
#import "UIColor+MYExtention.h"

@implementation UIColor (MYExtention)
+ (UIColor *)colorWithDecimalRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha
{
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}

+ (UIColor *)myC1
{
    return [UIColor colorWithDecimalRed:111 green:103 blue:100 alpha:1.0];
}

+ (UIColor *)myC2
{
    return [UIColor colorWithDecimalRed:150 green:144 blue:138 alpha:1.0];
}
@end

#import "UIColor+MYExtention.h"をhoge-Prefix.pchに追記しておけば、プロジェクトのどこからでもimport無しで利用できるようになる。
使うときはこんな感じ。

self.view.backgroundColor = [UIColor myC1];

同様に、フォントもカテゴリを作成しておくと何かと便利。

UIFont+MYExtention.h
@interface UIFont (MYExtention)
+ (UIFont *)myF200;
+ (UIFont *)myF160;
@end
UIFont+MYExtention.m
#import "UIFont+MYExtention.h"

@implementation UIFont (MYExtention)

+ (UIFont *)myF200
{
    return [UIFont fontWithName:@"AvenirNext-UltraLight" size:200];
}

+ (UIFont *)myF160
{
    return [UIFont fontWithName:@"AvenirNext-UltraLight" size:160];
}
@end
31
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
32