使用情节提要中的属性字符串本地化UILabel [英] Localising a UILabel with attributed string from a Storyboard

查看:45
本文介绍了使用情节提要中的属性字符串本地化UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在故事板上有一个UILabel,其文本设置为归因".当我生成 Main.strings 文件以翻译成其他语言时,该标签的文本不会出现.

I have a UILabel with text set as "attributed" in a storyboard. When I generate the Main.strings file for translating into a different language the text for this label does not appear.

我试图通过复制对象ID将条目手动添加到Main.strings文件中.我尝试设置文本"属性和"attributedText"属性,但是当我运行该应用程序时,未使用翻译后的字符串.

I tried to add manually an entry into the Main.strings file by copying the object id. I tried setting the "text" property and the "attributedText" property but when I run the app the translated string is not used.

那么,如何在情节提要板上本地化设置为归因"的UILabel?

So, how do I localise an UILabel set as "attributed" on a storyboard?

推荐答案

我最终使用首先,我在情节提要上使用的属性字符串仅对整个字符串应用了1.5行间距,因此在代码上设置文本以保留情节提要的标签格式更加容易.

To start with, the attributed string I used on the storyboard just applied a 1.5 line spacing to the whole string, so it was easier to set the text on code preserving the storyboard's label formatting.

情节提要包含一个UILabel,该UILabel的text属性设置为Attributed且高度为1.5行.

The storyboard contains a UILabel with the text property set as Attributed and a 1.5 line height multiple.

在我的视图控制器代码上,我有一个setupUI方法和该UILabel的出口.

On my view controller code I have a setupUI method and a outlet for that UILabel.

@interface MyClass ()
@property (weak, nonatomic) IBOutlet UILabel *aLabel;
@end

@implementation MyClass
- (void)setupUI {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.aLabel.attributedText];
    [attributedString.mutableString setString:NSLocalizedString(@"This is the label text which should be localisable.", "")];
    self.aLabel.attributedText = attributedString;
}
@end

使用上面的代码,变量attributedString存储了故事板上的所有格式设置,因此,当我更改attributedString文本时,无需再次设置格式.而且,当然,当我执行导出本地化"命令时,标签文本会显示在Localizable.strings上.

With the above code, the variable attributedString stores all the formatting set on the storyboard, so when I change the attributedString text I don't have to set the formatting again. And, of, course, the label text appears on the Localizable.strings when I execute the "Export for Localizations" command.

如果attributedString的内部子字符串格式不同,这将无法正常工作.在这种情况下,必须在代码上手动设置格式(或使用 Jelly 在评论中发布的答案所建议的 rtf 文件).

This wouldn't work as well if the attributedString had different formatting for its internal substrings. In that case the formatting would have to be set up manually on code (or using rtf files as suggested by the answer posted on the comment by Jelly).

这篇关于使用情节提要中的属性字符串本地化UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆