我需要将语言从英语更改为阿拉伯语。应用程序中的所有 UI 均以编程方式开发,我没有使用 Storyboard/xib 作为 UI。我使用本地化字符串来转换语言,但我正在寻找从 LTR 到 RTL 的 UI View ,反之亦然。
更改语言的最佳方法是什么,给我一些想法或示例
最佳答案
您可以通过以下方式查看当前语言:
open class func isCurrentLanguageRTL(language:String) -> Bool {
return Locale.characterDirection(forLanguage: language) == .rightToLeft
}
使用此 objective-c 代码(类别)您可以更改您的 UI RTL:
#import <Foundation/Foundation.h>
@interface NSBundle (Language)
+ (void)setLanguage:(NSString *)language;
@end
执行文件:
#import "NSBundle+Language.h"
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
static const char kBundleKey = 0;
@interface BundleEx : NSBundle
@end
@implementation BundleEx
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
NSBundle *bundle = objc_getAssociatedObject(self, &kBundleKey);
if (bundle) {
return [bundle localizedStringForKey:key value:value table:tableName];
}
else {
return [super localizedStringForKey:key value:value table:tableName];
}
}
@end
@implementation NSBundle (Language)
+ (void)setLanguage:(NSString *)language
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
object_setClass([NSBundle mainBundle], [BundleEx class]);
});
if ([self isCurrentLanguageRTL:language]) {
if ([[[UIView alloc] init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
[[UITableView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
[[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
}
}else {
if ([[[UIView alloc] init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[[UITableView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
}
[[NSUserDefaults standardUserDefaults] setBool:[self isCurrentLanguageRTL:language] forKey:@"AppleTextDirection"];
[[NSUserDefaults standardUserDefaults] setBool:[self isCurrentLanguageRTL:language] forKey:@"NSForceRightToLeftWritingDirection"];
[[NSUserDefaults standardUserDefaults] synchronize];
id value = language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil;
objc_setAssociatedObject([NSBundle mainBundle], &kBundleKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (BOOL)isCurrentLanguageRTL:(NSString *)code
{
return ([NSLocale characterDirectionForLanguage:code] == NSLocaleLanguageDirectionRightToLeft);
}
@end
使用方法:
任何类型的语言(如 RTL、LTR)仅在您的代码中调用此捆绑函数:
Bundle.setLanguage(<#Your Language code#>)
注意:您应该在桥接头文件中添加#import "NSBundle+Language.h"
。
如果你使用左侧菜单而不是 RTL,你必须从右侧打开菜单,只需检查上面的快速功能,如:
<#YourClass#>.isCurrentLanguageRTL(<#Your Language Code#>) {
//open your side menu from right
}
关于iOS:如何以编程方式将 UI 从 LTR 更改为 RTL,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54491246/