2015-03-19 60 views
2

我在故事板這麼多的UILabels和按鈕在相同的風格,我也有一個像如何在iOS中的故事板中使用編程定義的字體?

#define FLOAT_F1 [UIFont systemFontOfSize:18] 
#define FLOAT_F2 [UIFont systemFontOfSize:16] 

我可以在故事板使用這些定義?所以,我沒有去改變它一個字體列表如果標準發生了變化,則可以通過故事板中的一個。

+0

不,您可以創建一個繼承自UILabel的CustomLabel類。只需改變'awakeFromNib'方法中的字體並在storyboard中設置該類。 – 2015-03-19 06:29:45

+0

但是,這對我來說也很複雜,#_#,謝謝,我會盡力找到另一種方式。 – Rick 2015-03-19 06:49:57

+0

這很容易。這正是Jayesh發佈的。 – 2015-03-19 07:24:30

回答

1

您可以創建自定義標籤,如下所示。

文件:UICustomLabel.h

@interface UICustomLabel : UILabel 

@end 

文件:UICustomLabel.m

@implementation UICustomLabel 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 
- (id)initWithCoder:(NSCoder *)coder { 
    self = [super initWithCoder:coder]; 
    if (self) { 

     // [UIFont fontWithName:@"AmericanTypewriter-Bold" size:self.font.pointSize] 
     [self setFont:[UIFont fontWithName:@「Font_Name" size:self.font.pointSize]]; 
    } 
    return self; 
} 
@end 

那麼類UICustomLabel分配到你需要下面所示設置所有的故事板每個標籤。

Set Custom Class To Label

這會爲你工作。如果您有任何問題,請告訴我。

+0

謝謝,它的工作,但並不完美,例如,如果我需要視圖中的5種字體,我必須定製5個標籤。 – Rick 2015-03-19 07:43:33

+0

不,如果您有5個自定義標籤,則可以在單個類中製作五種不同類型的自定義標籤類,然後您可以使用該類。 – 2015-03-19 13:09:40

+0

這應該是正確的方法,我會分類我的標籤。 – Rick 2015-03-20 01:44:24

相關問題