因此,我嘗試以編程方式將應用程序語言從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 –