因此,我尝试以编程方式将应用程序语言从English
更改为RTL
语言。该文本已本地化,但布局仍为LTR
,直到我重新启动RTL
生效的应用程序。如何以编程方式将LTR改为RTL,反之亦然?
有没有办法让这种情况发生,而无需重新启动应用程序?
因此,我尝试以编程方式将应用程序语言从English
更改为RTL
语言。该文本已本地化,但布局仍为LTR
,直到我重新启动RTL
生效的应用程序。如何以编程方式将LTR改为RTL,反之亦然?
有没有办法让这种情况发生,而无需重新启动应用程序?
我结束了使用setSemanticContentAttribute
方法类似波纹管迫使RTL在运行时:
if ([NSLocale characterDirectionForLanguage:languageCode] == NSLocaleLanguageDirectionRightToLeft) {
if ([[[UIView alloc] init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
[[UIView appearance] setSemanticContentAttribute:
UISemanticContentAttributeForceRightToLeft];
}
}
else {
if ([[[UIView alloc] init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
}
在iOS应用程序中,通过调用userInterfaceLayoutDirectionForSemanticContentAttribute:方法获得UIView实例的布局方向。例如:
if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:view.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft) {
}
苹果提供的方式来支持从右到左这里的语言是链接: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/SupportingRight-To-LeftLanguages/SupportingRight-To-LeftLanguages.html
https://stackoverflow.com/a/41140436/6203030 –