React native - ios 中的 rtl

React native - rtl in ios

首先我在 android 上工作并且 RTL 运行良好, 我添加了以下代码:

   I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
   sharedI18nUtilInstance.setAllowRTL(context, true);

到MainActivity.java 和

android:supportsRtl="true"

到AndroidManifest.xml

在js代码中:

I18nManager.forceRTL(true);

现在的问题是: 我试图在 ios 上设置 rtl 但它不起作用 我添加了

 // in AppDelegate.m
   [[RCTI18nUtil sharedInstance] allowRTL:YES];

I18nManager.forceRTL(true);

在js代码中 但所有的文本和 flex 仍然是 ltr ... 我能做什么?

您必须在默认语言之外添加一种 RTL 语言,它才会起作用,可以找到更多信息 here

即使添加新的 RTL 语言,我的问题也没有解决。最后,我通过将以下行添加到我的 AppDelegate.m 文件中,在 didFinishLaunchingWithOptions 函数内,在 allow RTL[[RCTI18nUtil sharedInstance] allowRTL:YES]; 下解决了这个问题:

[[RCTI18nUtil sharedInstance] forceRTL:YES];

无需向您的项目添加 RTL 语言。只需打开您的 AppDelegate.m 文件 in yourProject/ios/YourProject/AppDelegate.m

注意不要打开另一个与此类似的扩展名为 .h 的文件

现在,将 #import <React/RCTI18nUtil.h> 添加到代码的顶部,如下所示:

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTI18nUtil.h>      <-- added here

现在重新启动并重建您的应用程序。

现在您可以使用 I18nManager 轻松更改整个应用程序的 RTL 方向。

import {I18nManager} from "react-native"


I18nManager.forceRTL(true) // false for LTR direction

在我的例子中添加

#import <React/RCTI18nUtil.h> //in top page AppDelegate.m

并在 didFinishLaunchingWithOptions 中将其添加到顶部 // 页面 AppDelegate.m

[[RCTI18nUtil sharedInstance] allowRTL:YES];

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
  [[RCTI18nUtil sharedInstance] allowRTL:YES];
...}