我有一个故事板,上面有一些文字视图。当我尝试本地化故事板(创建Storyboard.strings文件)时,所有UITextView都根本不进行本地化。 UILabels很好。
我的配置如下: 1个基本故事板文件,包含2个Storyboard.strings文件 Storyboard Configuration
Storyboard.strings文件没问题,毕竟它与UILabels合作: Storyboard.strings file
您是否找到了解决此问题的方案? 我正在使用Xcode 8.3,Swift 3,iOS 10。
答案 0 :(得分:2)
您可以在Xcode中创建一个IBOutlet,并在didSet函数中设置初始UITextView值。
E.g。
else
答案 1 :(得分:0)
我最终得到了这个解决方案:
在UIViewController中,我可以访问我的UITextView对象并强制使用Storyboard.strings中的本地化文本。
func fixTextViewStoryboardLocalization() {
guard storyboard != nil else {
return // No need to fix it if storyboard is not presented
}
let storyboardName = storyboard!.value(forKey: "name") as! String
for case let textView as UITextView in view.subviews {
if let ident = textView.restorationIdentifier {
textView.text = NSLocalizedString("\(ident).text", tableName: storyboardName, bundle: Bundle.main, value: "", comment: "")
}
}
}
创建自定义MyViewController(选择你想要的任何名称),在viewDidLoad中调用此函数:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
fixTextViewStoryboardLocalization()
}
}
确保故事板视图控制器来自MyViewController类: MyViewController custom class in Storyboard
为了使上面的代码有效,我需要在Storyboard中为每个UITextView设置Restoration ID: UITextView Restoration ID
最后一步 - 在Storyboard.strings中本地化文本视图:
/* Class = "UITextView"; text = "Base text"; ObjectID = "MyTextView-Restoration-ID"; */
"MyTextView-Restoration-ID.text" = "Localized text comes here";
这适用于我所有故事板中的所有UITextView。