ios - iOS:如何以编程方式将 UI 从 LTR 更改为 RTL,反之亦然
问题描述
我需要将语言从英语更改为阿拉伯语。应用程序中的所有 UI 都是以编程方式开发的,我没有使用 Storyboard/xib 作为 UI。我使用本地化字符串来转换语言,但我正在寻找从 LTR 到 RTL 的 UI 视图,反之亦然。
改变语言的最佳方法是什么,并给我一些想法或示例
解决方案
您可以通过以下方式检查当前语言:
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
}
推荐阅读
- swift - 根据用户在 Swift 中选择的日期更改显示的数据?
- css - CSS:媒体如何计算它的图像大小
- javascript - 递归 setTimeouts 对性能不利吗?
- drake - 限制浮动基础四元数位置和角速度的推荐方法是什么?
- macos - 在 macOS 上确定 PDF 文档中的渲染高度以绘制动态大小的页脚
- python - 使用带有 Selenium 的 webdriver 拒绝访问网站
- c# - Entity Framework 6.2 不应自动获取我的自定义 DbConfiguration
- excel - EXCEL - 在工作表中的每个工作表中替换工作表中的相同单词
- postgresql - 未使用为 PostgreSQL jsonb 列创建的索引
- docker - 如何使用 Azure DevOps 将 JFrog Artifactory 中的映像部署到 Azure Kubernetes 服务中