Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: How to change all fonts at once in an app?

I was wondering if it was possible to change the fonts on about 100 different viewcontrollers all at once? It would be a lot eisier than going through one by one and changing them. Any ideas? Thank you!

like image 217
ToxicProxy Avatar asked Dec 05 '22 10:12

ToxicProxy


2 Answers

The user interface files (*.xib) are plain text and you can load them into an editor.

In Xcode4 on the left pane you can right-click > open as > source.

This will give you the XML source any you can find/replace there.

Warning: doing something wrong may render the whole file useless, so unless you have source control anyway, make copies of the XIB before attempting changes.

like image 125
Terminality Avatar answered Dec 17 '22 08:12

Terminality


you cant change all the fonts at once....

But i have find one more varient that will help you...

I have made some recursive functions thy can help you..

follow following steps..

First create a class(BaseViewController) extended from UIViewController like in BaseViewController.h file

@interface BaseViewController : UIViewController

And in BaseViewController.m file write following code.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self changeFontsOfViewController];
}

-(void)changeFontsOfViewController
{
    UIViewController * vv = [self viewControllerOfView:self.view];
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([vv class]) owner:vv options:nil];

    for (id object in objects)
    {
        [self changeFontOfView:object];
    }

}

-(void)changeFontOfView:(UIView *)aView
{
    for (UIView *vv in [aView subviews])
    {

        if ([vv isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)vv;
            CGFloat fontSize = btn.titleLabel.font.pointSize;
            btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize];
        }
        else if ([vv isKindOfClass:[UILabel class]])
        {
            UILabel *lbl = (UILabel *)vv;
            CGFloat fontSize = lbl.font.pointSize;
            [lbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UITextView class]])
        {
            UITextView *txt = (UITextView *)vv;
            CGFloat fontSize = txt.font.pointSize;
            [txt setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UITextField class]])
        {
            UITextField *txt = (UITextField *)vv;
            CGFloat fontSize = txt.font.pointSize;
            [txt setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UIView class]]||[vv isKindOfClass:[UIScrollView class]])
        {
            if (aView.subviews.count == 0)return;
            [self changeFontOfView:vv];
        }
    }

}

Now your every viewController(RootViewController) will be extended from BaseViewController class like in RootViewController.h..

#import "BaseViewController.h"

@interface RootViewController : BaseViewController
{

}

And make sure that you have written following in your .m file of your UIViewController(RootViewController.m)

- (void)viewDidLoad
{
    [super viewDidLoad];
}

Please follow above steps carefully you will rock.......

like image 25
Nirav Gadhiya Avatar answered Dec 17 '22 07:12

Nirav Gadhiya