2

I would like to set custom font for my complete view

I have referred the following How do I set a custom font for the whole application?

But this suggests only font type and font size for the labels currently am using following code snippet for all labels which is increasing the redundancy.

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

Is it possible to add text color and background color in similar fashion ?

Community
  • 1
  • 1
DNamto
  • 1,342
  • 1
  • 21
  • 42
  • This question may be a duplicate of something else, but it's not a duplicate of the question it's marked as. – nevan king Mar 20 '13 at 00:03

3 Answers3

3

Try this

-(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];
        }
    }
}

call this methode and pass your view

Chirag Patel
  • 11,416
  • 3
  • 26
  • 38
2

If you're using iOS 5+ and have the labels inside a custom view (or can put them into a custom view) then you can use UIAppearance and UIAppearanceContainer. They are made for exactly this scenario.

The relevant method is +appearanceWhenContainedIn:, which allows you to set the appearance (font, color, background image etc.) when the view class you are targeting is contained inside a view of a given class.

// 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]];

I put this into the app delegate, but you could put it somewhere else. This will change the appearance of all UILabel instances which are inside a view of class MyView.

Check out the WWDC 2011 video for Session 114 for details.

enter image description here

nevan king
  • 112,709
  • 45
  • 203
  • 241
1

Create a category for label like"

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

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

Now call setCustomLabelDesign with your label (as [label setCustomLabelDesign]) to customize that in this accordance.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109