LoginSignup
12
12

More than 5 years have passed since last update.

僕がiOSアプリを作るときにやってるUILabel管理

Posted at

僕がやってるUILabel管理を晒します。もっとこうした方がいいよ等ございましたら、コメントしてください。
フォントと文字の色を毎回指定するのが面倒だったからこうしました。

ファイル作成

スクリーンショット 2013-11-19 16.29.16.png
スクリーンショット 2013-11-19 16.30.51.png

UILabelを継承したBasicLabelファイルを作ります。

BasicLabel.h

BasicLabel.h
#import <UIKit/UIKit.h>

typedef enum : NSInteger{
    HeaderTitle = 0,
    NavigationMenu,
    TableCellName
}BasicLabelName;

@interface BasicLabel : UILabel
- (id)initWithName:(BasicLabelName)name;
@end

BasicLabel.m

BasicLabel.m
#import "BasicLabel.h"

@implementation BasicLabel

- (id)initWithName:(BasicLabelName)name {

    self = [super init];
    if (self) {
        switch (name) {
            case HeaderTitle:{
                self.textColor = [UIColor colorWithRed:0.188 green:0.239 blue:0.314 alpha:1.0];
                UIFont *font = [UIFont fontWithName:@"HiraKakuProN-W3" size:14.0f];
                self.font = font;
                break;
            }
            case NavigationMenu:{
                self.textColor = [UIColor colorWithRed:0.427 green:0.427 blue:0.447 alpha:1.0];
                UIFont *font = [UIFont fontWithName:@"HiraKakuProN-W3" size:12.0f];
                self.font = font;
                break;
            }
            case TableCellName:{
                self.textColor = [UIColor whiteColor];
                UIFont *font = [UIFont fontWithName:@"HiraKakuProN-W3" size:14.0f];
                self.font = font;
                break;
            }
            default:
                break;
        }
    }
    return self;
}
@end

使い方

#import "BasicLabel.h"   //作成したBasicLabel.hを読み込む

//実際に使うとき
BasicLabel *headerLabel = [[BasicLabel alloc]initWithName: HeaderTitle];
[headerLabel setText:@"ヘッダータイトル"];
[headerLabel sizeToFit];
headerLabel.frame = CGRectMake(10, 10, headerLabel.frame.size.width, headerLabel.frame.size.height);
12
12
1

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
12
12