僕がやってるUILabel管理を晒します。もっとこうした方がいいよ等ございましたら、コメントしてください。
フォントと文字の色を毎回指定するのが面倒だったからこうしました。
ファイル作成
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);