【问题标题】:how set custom font for whole all labels in a view? [duplicate]如何为视图中的所有标签设置自定义字体? [复制]
【发布时间】:2013-03-08 02:34:33
【问题描述】:

我想为我的完整视图设置自定义字体

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

但这仅建议标签的字体类型和字体大小 目前正在对所有增加冗余的标签使用以下代码 sn-p。

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

是否可以以类似的方式添加文本颜色和背景颜色?

【问题讨论】:

  • 此问题可能与其他问题重复,但与它所标记的问题不同。

标签: ios xcode fonts uilabel


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案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 实例的外观。

    查看 WWDC 2011 视频以了解第 114 场会议的详细信息。

    【讨论】:

      【解决方案3】:

      label 类似“创建category

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

      现在使用您的标签(如[label setCustomLabelDesign])调用setCustomLabelDesign,以按照此自定义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-12
        • 1970-01-01
        • 2013-08-23
        • 2017-11-16
        • 1970-01-01
        • 1970-01-01
        • 2018-02-15
        • 2023-03-07
        相关资源
        最近更新 更多