- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将语言从英语更改为阿拉伯语。应用程序中的所有 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/
iOS 应用程序,我们要显示来自服务器的新闻。使用UIlabel 当他只用一种语言而不考虑布局时,一切都是完美的(我们正在将布局 RTL 切换为 LTR,以适应不同的语言,包括阿拉伯语、希伯来语) 当
新版本的 React Native 已经发布了对 RTL 设备的支持: https://facebook.github.io/react-native/blog/2016/08/19/right-to
我将 Material UI 5 与 next.js 一起使用,并使用 emotion 和 stylis-plugin-rtl v2 实现了与此处文档完全相同的每个步骤: https://next.m
我知道如何在方向为内联时设置样式 foo div[dir="rtl"] { ....; } 但是如何设置样式 foo ? 两者的行为都符合预期(文本的右“对齐”),但我需要对内部的某些元素进行更精细
这个问题在这里已经有了答案: What is use of android:supportsRtl="true" in AndroidManifest xml file (4 个回答) 关闭4年前。
我正在使用material-ui和next和jss-rtl在我的react项目但是有一个问题页面仍然是ltr使用后 -rtl组件代码: import React from "react"; impor
我敢发誓,vivado 有一个错误,它永远不会刷新对 RTL 文件、verilog 或 vhdl 所做的任何接口(interface)更改,在使用“添加模块”将其粘贴到“ block 设计”后....
我正在尝试将 react Material 模板添加到我的 reactJs 项目中。我修复了所有错误,但我无法修复这个错误 WARNING in ./node_modules/stylis-plugi
我在 android 中构建一个绘图应用程序,我在绘制矩形时注意到一个问题。应该根据语法绘制矩形 new Rect(left, top, right, bottom) 现在我的逻辑通过取起点 (sta
我将 Tinymce 编辑器安装到我的网站。现在我需要将默认方向设置为 RTL 而不是 LTR。您可以在下面看到-- tinymce.init({ selector: 'textar
我注意到没有 bottomEndRadius在形状中,而只是 bottomRightRadius 是否有任何解决方法来支持从右到左的布局? 最佳答案 我发现的唯一方法是为 R
我使用此代码来运行具有 RTL 支持的 Isotope $.Isotope.prototype._positionAbs = function( x, y ) { return { right:
尝试显示以数字开头的希伯来语字符串,总是在字符串末尾显示数字,如下所示:1. йום שйשй בבוקר 但我需要在文本右侧显示数字- 有什么解决办法吗? 它发生在 UILabel & UIText
我在 react-native 中创建了一个应用程序,当电话语言为 En 时一切正常,但是当我更改电话语言时,UI 崩溃并且一切都向后 我在用 "react": "16.6.3", "reac
WordPress 非常强大......但对于阿拉伯语和希伯来语等 RTL 语言却很弱......我需要让前端使用阿拉伯语,而管理区域则需要使用英语,当然还有 LTR。如果我使用阿拉伯语版本的Word
我想知道为什么将Camera2 API与RTL语言一起使用时,摄像机预览会有所不同。具体来说,我有两个问题: 使用RTL时,相机上是否有任何特定设置? 我正在通过OpenCV使用边缘检测。 OpenC
我正在尝试在阿拉伯语 RTL 页面中使用 jQuery 菜单。我可以将其设为 RTL,但图标应该是 ui-icon-carat-1-w 而不是 ui-icon-carat-1-e。我创建了jsfidd
我正在尝试在统一输入字段中编写波斯语。波斯语是从右到左的语言,其字母与阿拉伯语相似。 我找到了 library转换和更正文本。它工作得很好。当我将转换后的文本显示为 Debug.Log() 时,一切都
我的应用程序支持 4 种不同的语言,其中两种是从右到左编写的。 如果我从 android 系统更改语言然后启动我的应用程序,一切正常。我得到一个镜像布局 (RTL),甚至导航栏也是镜像的。 我的问题是
我正在设计一种布局,该布局在不同的 View 中同时包含英语和希伯来语(旨在从右到左)文本。当一行希伯来语文本超过一定长度时,它会变成从左到右书写(我认为这与长度有关,因为它在较短的文本上看起来很好,
我是一名优秀的程序员,十分优秀!