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.

没有正确的解决方案

其他提示

I actually have done this by adding different font files for different languages in the project and in the plist against UIAppFonts. Then I introduced:

"fontName" = "English Font";
"fontName" = "Russian Font";

in Localizable.strings for each supported language

I then accessed it using:

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

As I mentioned in my question that the other two approaches seem to be more elegant. Any solution regarding those will be highly helpful.

You are on the right track with UIAppFonts. First, add the .ttf files to your project under the Resources folder. Then, you need to add a new line to the Info.plist (not InfoPlist.strings?). You do this by clicking on the Plus sign (+) at the far right of the last line in the plist, or I think You can Control-click any other property and select New. Select UIAppFonts from the drop-down in the first column, and that should put an arrow (>) to the left of it. Click on this arrow to expand the property and you should see item0, type in the name of your .ttf file there. Control-click to add more items as necessary.

This will add your custom fonts to the application. It is important to note that when you load them using fontWithName, the name you supply in @"font-name" may not (and usually IS NOT) the exact same name as the .ttf file. Rather, it is the whole name as it appears in the Font Book under Preview --> Show Font Info.

All this is fine, but it doesn't localize anything. You could use something like the line below to query the language code and then decide which font to load based on that:

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top