Fel*_*rri 11 localization objective-c nsattributedstring ios
我有一个UILabel,文本在故事板中设置为"归属".当我生成Main.strings文件以翻译成另一种语言时,不会出现此标签的文本.
我试图通过复制对象id手动将一个条目添加到Main.strings文件中.我尝试设置"text"属性和"attributedText"属性,但是当我运行应用程序时,不使用已翻译的字符串.
那么,如何将UILabel集本地化为故事板上的"归属"?
我最后用故事板和代码的组合来解决这个问题,并将此答案作为参考.
首先,我在故事板上使用的属性字符串只对整个字符串应用了1.5行间距,因此在代码上设置文本更容易,保留了故事板的标签格式.
故事板包含一个UILabel,其text属性设置为Attributed,高度为1.5行高.
在我的视图控制器代码上,我有一个setupUI方法和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
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,变量attributedString存储故事板上设置的所有格式,因此当我更改attributedString文本时,我不必再次设置格式.当然,当我执行"导出本地化"命令时,标签文本出现在Localizable.strings上.
如果referencedString的内部子字符串具有不同的格式,那么这也不会起作用.在这种情况下,格式化必须在代码上手动设置(或使用rtf文件,如Jelly评论中发布的答案所示).
| 归档时间: |
|
| 查看次数: |
3587 次 |
| 最近记录: |