2013-03-19 61 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

相关问题