首页 > 解决方案 > iOS:如何以编程方式将 UI 从 LTR 更改为 RTL,反之亦然

问题描述

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

改变语言的最佳方法是什么,并给我一些想法或示例

标签: iosiphoneuser-interfaceswift4arabic

解决方案


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

open class func isCurrentLanguageRTL(language:String) -> Bool {
        return Locale.characterDirection(forLanguage: language) == .rightToLeft
    }

使用这个目标 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
}

推荐阅读