2013-03-19 60 views
2

我想設置自定義字體爲我的完整視圖如何爲視圖中的所有標籤設置自定義字體?

我已經提到了以下 How do I set a custom font for the whole application?

但是這表明了標籤 目前正在使用下面的代碼對所有標籤隻字體類型和大小這增加了冗餘。

label.textColor = [UIColor blueColor]; 
[label setBackgroundColor:[UIColor clearColor]]; 

是否有可能以類似的方式添加文本顏色和背景顏色?

+0

這個問題可能是別的東西重複,但它不是它被標記爲問題的副本。 – 2013-03-20 00:03:20

回答

3

試試這個

-(void)changeFont:(UIView *) view{ 
    for (id View in [view subviews]) { 
     if ([View isKindOfClass:[UILabel class]]) { 
       [View setFont:[UIFont fontWithName:@"Candara" size:26]]; 
       View.textColor = [UIColor blueColor]; 
       [View setBackgroundColor:[UIColor clearColor]]; 
     } 
     if ([View isKindOfClass:[UIView class]]) { 
      [self changeFont:View]; 
     } 
    } 
} 

調用此梅索德並通過您view

1

創建label一個category像」

@interface UILabel(SMLabelCatogary) 
    - (void) setCustomLabelDesign; 
    @end 

    @implementation UILabel(SMLabelCatogary) 
    - (void) setCustomLabelDesign{ 
     label.textColor = [UIColor blueColor]; 
     [label setBackgroundColor:[UIColor clearColor]]; 
    } 
    @end 

現在叫setCustomLabelDesign與您的標籤(如[label setCustomLabelDesign])定製在這個按照。

2

如果您使用iOS 5+並將標籤放在自定義視圖中(或者可以將它們放入自定義視圖中),則可以使用UIAppearanceUIAppearanceContainer。他們正是爲了這個場景而製作的。

相關的方法是+appearanceWhenContainedIn:,它允許您設置外觀(字體,顏色,背景圖像等),當您定位的視圖類包含在給定類的視圖內。

// Set appearance of UILabels within MyViews 
[[UILabel appearanceWhenContainedIn:[MyView class], nil] 
    setTextColor:[UIColor greenColor]]; 

[[UILabel appearanceWhenContainedIn:[MyView class], nil] 
    setFont:[UIFont fontWithName:@"SourceCodePro-Black" size:24]]; 

我把它放到應用程序委託中,但你可以把它放在別的地方。這將改變MyView類視圖內的所有UILabel實例的外觀。

有關詳細信息,請參閱第114課的WWDC 2011視頻。

enter image description here

相關問題