2017-04-06 40 views
0

我的故事板上有一些文字視圖。當我嘗試本地化故事板(創建Storyboard.strings文件)時,所有UITextView都不會本地化。 UILabels很好。iOS UITextView不能在storyboard中正確定位

我的配置情況如下: 1個帶2個Storyboard.strings文件故事板文件 Storyboard Configuration

Storyboard.strings文件確定,它與UILabels工作畢竟: Storyboard.strings file

你有沒有發現這個問題的解決方案? 我使用Xcode 8.3,Swift 3,iOS 10.

回答

0

您可以在Xcode中創建一個IBOutlet,並在didSet函數中設置初始UITextView值。

E.g.

@IBOutlet var textView: UITextView { 
    didSet { 
     textView.text = NSLocalizedString("CUSTOM_LOCALISED_STRING", comment: "Comment.") 
    } 
} 
+0

我非常想在Storyboard中進行本地化。您的解決方案迫使我將本地化的一些部分存儲在Localizable.strings中。 – user2552888

0

我結束了此解決方案:

在一個UIViewController我可以從Storyboard.strings訪問我的UITextView的對象,並迫使本地化的文本中使用。

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設置恢復ID: UITextView Restoration ID

最後一步 - 本地化Storyboard.strings文本視圖:

/* Class = "UITextView"; text = "Base text"; ObjectID = "MyTextView-Restoration-ID"; */ 
"MyTextView-Restoration-ID.text" = "Localized text comes here"; 

這適用於我在我所有的故事板所有UITextViews。