iOS Storyboard localizable strings do not work on UILabel subclasses

人盡茶涼 提交于 2019-11-28 04:11:50

问题


I'm using the new iOS functionnality to translate the storyboards (http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/InternationalizeYourApp/InternationalizeYourApp/InternationalizeYourApp.html)

The problem with this solution, it does not work with my UILabel subclasses.

Here are the codes for my UILabel subclasses :

.h:

#import <UIKit/UIKit.h>
@interface LabelThinText : UILabel
- (void)awakeFromNib;
-(id)initWithFrame:(CGRect)frame;
@end

.m :

@implementation LabelThinText

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setFont:[UIFont fontWithName:@"Raleway-Light" size:[[self font] pointSize]]];
}

-(id)initWithFrame:(CGRect)frame
{
    id result = [super initWithFrame:frame];
    if (result) {
        [self setFont:[UIFont fontWithName:@"Raleway-Light" size:[[self font] pointSize]]];
    }
    return result;
}

@end    

I guess i'm missing something to get the automatic translations from my Storyboard.strings file.

Anyone has an idea ?

Thanks !


回答1:


I ran into the same problem, and for the same reason, setting a custom font in a label. My strategy was a little more general, though. My custom font is Helvetica-like, so I used Helvetica Neue in IB as a placeholder font. The UILabel subclass translated that to my custom font, preserving font size and weight, so I could control all that through IB.

That made my workaround for the translation bug (I assume it's a bug) easier. I traverse all my views recursively in viewDidLoad, and map all UILabel fonts if they match the placeholder font, and the same for UIButton in my case.

Have you filed a bug?




回答2:


Encountered the same problem, here's how I got around it:

Drop your custom class, and create a category on UILabel, in your case UILabel+ThinText:

- (void) setThinText:(BOOL)thinText
{
    if (thinText) {
        [self setFont:[UIFont fontWithName:@"Raleway-Light" size:[[self font] pointSize]]];
    }
}

In your storyboard, select your label, choose the Identity Inspector and add the following User Defined Runtime Attribute:

Keypath: thinText – Type: Boolean – Value: checked




回答3:


I got this issue too. I solve it setting the custom font in each ViewController. Let me show you an example:

 CustomViewController.h
@interface CustonViewController

@property (strong, nonatomic) IBOutlet UILabel* someLabel;

@end

The in CustomViewController.m

- (void)viewDidLoad
{
  [self setUoFonts];
}

- (void)setUpFonts
{
    self.someLabel.font = [UIFont fontWithName:@"AmaticSC-Regular" size:self.someLabel.font.pointSize];  
}

And that's it!. You're going to have your translation and your custom font.

Remember to remove the custom class from StoryBoard.



来源:https://stackoverflow.com/questions/16928670/ios-storyboard-localizable-strings-do-not-work-on-uilabel-subclasses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!