iOS Storyboard可本地化的字符串不适用于UILabel子类

时间:2013-06-04 22:38:53

标签: ios fonts localization translation uistoryboard

我正在使用新的iOS功能来翻译故事板(http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/InternationalizeYourApp/InternationalizeYourApp/InternationalizeYourApp.html

此解决方案的问题是,它不适用于我的UILabel子类。

以下是我的UILabel子类的代码:

·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    

我想我错过了从Storyboard.strings文件中获取自动翻译的内容。

有人有想法吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,出于同样的原因,在标签中设置自定义字体。不过,我的策略有点笼统。我的自定义字体是Helvetica,所以我在IB中使用Helvetica Neue作为占位符字体。 UILabel子类将其转换为我的自定义字体,保留了字体大小和重量,因此我可以通过IB控制所有这些。

这使得我对翻译错误的解决方法(我认为这是一个错误)更容易。我在viewDidLoad中递归遍历所有视图,如果它们匹配占位符字体,则映射所有UILabel字体,在我的情况下,UIButton字体也相同。

你有提交错误吗?

答案 1 :(得分:1)

遇到同样的问题,这就是我如何解决它:

删除自定义类,并在UILabel上创建一个类别,例如UILabel + ThinText:

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

在故事板中,选择您的标签,选择 Identity Inspector 并添加以下用户定义的运行时属性

Keypath :thinText - 类型:布尔 - :已选中

答案 2 :(得分:0)

我也遇到了这个问题。我解决了在每个ViewController中设置自定义字体的问题。让我举个例子:

 CustomViewController.h
@interface CustonViewController

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

@end

in CustomViewController.m

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

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

就是这样!您将获得翻译和自定义字体。

请记住从StoryBoard中删除自定义类。