Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Storyboard localizable strings do not work on UILabel subclasses

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 !

like image 400
Arnaud Avatar asked Nov 02 '22 20:11

Arnaud


2 Answers

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?

like image 183
JLundell Avatar answered Nov 09 '22 11:11

JLundell


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

like image 27
Stephane JAIS Avatar answered Nov 09 '22 13:11

Stephane JAIS