Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use different fonts for different languages in an iOS application?

I am in a scenario where I want to use different fonts for the different languages in my iOS application.

At first, I thought of using localized versions of .ttf files. I tried adding the localized .ttf files in the localized project folders for different languages with the same filename and font-name installed in them. It didn't work.

I then looked into the InfoPlist.strings files for different languages. It appeared to me as the key value combination for the 'key' strings found in the main Info.plist. I found the font entry against the key 'UIAppFonts' which was an array. I am unable to figure out how to put the localized font file names in the InfoPlist.strings as an array.

As a last resort, I am thinking of adding localized font names in the Localizable.stings files to pick the font name according to the current locale and replace all the occurrences in my project wherever I have used fonts. (Obviously it is a lot cumbersome). Like:

UIFont *font = [UIFont fontWithName:NSLocalizedString(@"font-name", nil) size:16];

Please assist if anybody has done this before. It would be great if somehow one of the first two ideas can be implemented.

like image 401
sajid.chingchong Avatar asked Aug 27 '12 04:08

sajid.chingchong


People also ask

How do I put custom fonts on my iPhone apps?

You can download fonts from the App Store app , then use them in documents you create on iPhone. After you download an app containing fonts from the App Store, open the app to install the fonts. To manage installed fonts, go to Settings > General, then tap Fonts.

How do I use custom fonts in Xcode?

To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.


1 Answers

To use specific UIFonts inside an app I created an extension of UIButton and UILabel. Because I had the same problem, choosing different UIFonts for different languages, I also inserted a check into my extension. What looked like:

@implementation MyButton{
    NSString *_language;
}

- (void)awakeFromNib{
    
    _language = [[NSLocale preferredLanguages] objectAtIndex:0];        
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.titleLabel setFont:[self whichFontShouldBeUsed]];
}

- (UIFont *)whichFontShouldBeUsed{
    
    UIFont *fontToUse = [UIFont fontWithName:@"Basic Font"
                                        size:self.titleLabel.font.pointSize];
    
    if( [_language isEqualToString:@"tr"] &&
        [_language isEqualToString:@"pl"] ){
        fontToUse = [UIFont fontWithName:@"Another Font" 
                                    size:self.titleLabel.font.pointSize];
    } else if( [_language isEqualToString:@"ja"] &&
               [_language isEqualToString:@"tr"] ){
        
        fontToUse = [UIFont boldSystemFontOfSize:self.titleLabel.font.pointSize];
    } else if( [_language isEqualToString:@"ru"] ){
        fontToUse = [UIFont fontWithName:@"Another Font"
                                    size:self.titleLabel.font.pointSize + 2.0f];
    }
    
    return fontToUse;
}

This way you can always switch UIFonts automatically. Next, you just have to add this class inside the Interface Builder. Select the UIButton you want and add your custom UIButton class:

enter image description here

For UILabel you would use setText instead of setTitleLabel" and setTextColorinstead ofsetTitleColor`.

For further information to preferredLanguages look at this website and of course don't forget to add your fonts to the projects Target and also to the plist.

like image 164
Alex Cio Avatar answered Nov 16 '22 03:11

Alex Cio