iOS:如何以编程方式将 UI 从 LTR 更改为 RTL,反之亦然

iOS: how to change Programmatically UI from LTR to RTL and viceversa

我需要将语言从英语更改为阿拉伯语。应用程序中的所有 UI 都是以编程方式开发的,我没有将 storyboard/xib 用于 UI。我使用本地化字符串来转换语言,但我正在寻找从 LTR 到 RTL 的 UI 视图,反之亦然。

语言转换的最佳方式是什么并给我一些想法或示例

您可以通过以下方式查看当前语言:

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,则必须从右侧打开菜单,只需检查上面的 swift 功能,例如:

<#YourClass#>.isCurrentLanguageRTL(<#Your Language Code#>) {
  //open your side menu from right
}