iOS 阿拉伯语UI适配规则

2 阅读6分钟

iOS 阿拉伯语(RTL)页面适配规范

适用于 UIKit + SnapKit 工程,SwiftUI 部分见第 7 节。

目标:同时支持两种场景——跟随系统语言、用户在 App 内手动切换语言——且两种场景下布局方向始终与 App 当前展示的语言一致。


0. 核心原则(先读这一节)

  1. 布局方向只有一个判断源:App 当前展示语言是否为 RTL(AppLayoutDirection.isRTL)。所有代码只问它,不问系统。

  2. 永远强制方向,不依赖系统推断:LTR 也要显式设成 .forceLeftToRight,不能留 .unspecified。

原因:系统是阿语、用户在 App 里选了英语时,系统会把 .unspecified 的视图当成 RTL 排版。

  1. 禁止用系统级方向 API 做判断:
  • ❌ UIApplication.shared.userInterfaceLayoutDirection(只反映系统语言 + 工程本地化,不感知 App 内切换)

  • ❌ traitCollection.layoutDirection(同上)

  • ❌ UIImage.imageFlippedForRightToLeftLayoutDirection()(依赖 trait,App 内切换后不准)

  • ✅ AppLayoutDirection.isRTL,或视图自身的 effectiveUserInterfaceLayoutDirection(视图已被强制方向时可信)

  1. 能交给系统就交给系统:约束一律 leading/trailing,文字一律 .natural。手动判断 isRTL 只用于系统翻不了的场景(见第 4 节)。

  2. 切换方向 = 重建界面:appearance 只影响之后创建的视图,已存在的视图不会自动翻转。


1. 语言与方向的决策

1.1 语言决策优先级


用户在 App 内的手动选择(持久化) > 系统首选语言(且 App 支持) > 默认语言(en)

  • 用户选择「跟随系统」时,清除持久化的选择,回落到系统语言。

  • 系统语言取 Bundle.main.preferredLocalizations.first(已按 App 支持的本地化匹配过),或 Locale.preferredLanguages.first 再自行匹配。

  • RTL 语言集合:ar(阿拉伯语)、he(希伯来语)、fa(波斯语)、ur(乌尔都语)。按工程实际支持的语言裁剪。

1.2 方向判断源(模板)


enum AppLayoutDirection {

private static let rtlLanguageCodes: Set<String> = ["ar", "he", "fa", "ur"]

/// 唯一判断源:App 当前展示语言是否 RTL

static var isRTL: Bool {

let code = AppLanguage.current.code // 工程自己的「当前语言」

let base = code.split(separator: "-").first.map(String.init) ?? code

return rtlLanguageCodes.contains(base)

}

static var semanticAttribute: UISemanticContentAttribute {

isRTL ? .forceRightToLeft : .forceLeftToRight

}

/// 按方向取值

static func value<T>(ltr: T, rtl: T) -> T { isRTL ? rtl : ltr }

}

1.3 全局应用方向(模板)


extension AppLayoutDirection {

/// 启动、手动切换语言后都要调用;之后必须重建界面(见第 2 节)

@discardableResult

static func applyGlobally() -> UISemanticContentAttribute {

let attribute = semanticAttribute

UIView.appearance().semanticContentAttribute = attribute

UINavigationBar.appearance().semanticContentAttribute = attribute

UISearchBar.appearance().semanticContentAttribute = attribute

UICollectionView.appearance().semanticContentAttribute = attribute

UITabBar.appearance().semanticContentAttribute = attribute

return attribute

}

}


2. 两种语言场景的处理

2.1 场景 A:跟随系统语言

| 时机 | 处理 |

|---|---|

| 首次启动 / 冷启动 | 按 1.1 解析语言 → applyGlobally() → 再创建 window 和根控制器 |

| 用户在「设置」里改系统语言或 App 单独语言(iOS 13+) | 系统会杀掉并重启 App,走冷启动流程即可,无需监听 |

| 系统是阿语,App 不支持阿语 | 1.1 回落到默认语言 → 强制 LTR。不要让系统把界面翻成 RTL |

要点:

  • applyGlobally() 必须在创建任何视图之前调用(application(_:didFinishLaunchingWithOptions:) 或 scene(_:willConnectTo:) 里,早于 window.rootViewController 赋值)。

  • 根导航控制器的 view.semanticContentAttribute 也要显式设置,侧滑返回手势的方向依赖它。

  • Info.plist 的 CFBundleLocalizations 需包含 ar,并提供 ar.lproj,否则系统层面(如「设置」里的 App 语言选项)不会出现阿语。

2.2 场景 B:用户在 App 内手动切换


func switchLanguage(to language: AppLanguage) {

AppLanguage.setCurrent(language, persist: true) // 1. 持久化 + 切换本地化 Bundle

let attribute = AppLayoutDirection.applyGlobally() // 2. 更新全局方向

rebuildRootInterface(semanticAttribute: attribute) // 3. 重建界面

NotificationCenter.default.post(name: .appLanguageDidChange, object: nil) // 4. 通知未重建的对象

}

func rebuildRootInterface(semanticAttribute: UISemanticContentAttribute) {

guard let window = UIApplication.shared.connectedScenes

.compactMap({ ($0 as? UIWindowScene)?.keyWindow }).first else { return }

let root = makeRootViewController() // 重新创建,不要复用旧实例

root.view.semanticContentAttribute = semanticAttribute

window.rootViewController = root

UIView.transition(with: window, duration: 0.25, options: .transitionCrossDissolve, animations: nil)

}

要点:

  • 必须重建:旧视图的 semanticContentAttribute 已在创建时定型,改 appearance 不会回溯。重建根控制器是最可靠的方式;如需保留导航栈,就逐个重新创建栈内控制器后再 setViewControllers。

  • 单例 / 缓存要清理:持有视图的单例(悬浮窗、全局弹窗容器、Toast 容器)、缓存的富文本、缓存的翻转图片、预渲染的 cell 高度,都要在切换后失效或重建。

  • 长生命周期组件:灵动岛 / Live Activity、Widget、通知扩展各自有独立进程,需要在切换后主动刷新或结束重建。

  • 系统控件不受控:键盘、UIAlertController 的按钮顺序、分享面板、系统日期选择器等,仍按系统语言显示方向。这属于可接受的差异,不要去 hack。

2.3 两种场景的统一检查点

  • 所有判断都走 AppLayoutDirection.isRTL,两种场景天然一致。

  • 「系统阿语 + App 内选英语」和「系统英语 + App 内选阿语」这两个交叉组合必须各测一遍。


3. 系统自动翻转的部分(只需写对 API)

强制方向后,以下内容由 UIKit 自动镜像,前提是 API 用对:

| 控件 / 能力 | 正确写法 | 错误写法 |

|---|---|---|

| Auto Layout / SnapKit 约束 | make.leading / make.trailing、leadingMargin、directionalLayoutMargins | make.left / make.right、layoutMargins.left |

| UIStackView | 水平排列自动从右往左,无需处理 | 手动 reverse() 子视图 |

| UILabel | textAlignment = .natural(默认值) | .left(会固定在左) |

| UITextField / UITextView | textAlignment = .natural | .left |

| UINavigationBar | 返回按钮、左右 item 自动对调 | 手动交换 leftBarButtonItem / rightBarButtonItem |

| 侧滑返回手势 | 方向随导航控制器 view.semanticContentAttribute 自动反转 | — |

| UITableView | cell 内容按约束自动翻转;accessoryType 自动换边 | — |

| UICollectionViewCompositionalLayout | 自动镜像 | — |

| NSDirectionalEdgeInsets | leading / trailing 自动对应 | UIEdgeInsets.left/right(不翻转) |

| UIButton.Configuration | imagePlacement = .leading/.trailing 自动翻转 | contentHorizontalAlignment = .left |

判断标准:凡是 API 里出现 left / right 的,都不会翻转;出现 leading / trailing / natural 的,都会翻转。


4. 需要手动处理的控件规则

4.1 约束与内边距

| 场景 | 规则 |

|---|---|

| 约束 | 一律 leading/trailing。只有「物理方向」才用 left/right(例如:始终贴屏幕左侧的悬浮按钮——这种情况极少,需注释说明) |

| UIEdgeInsets(按钮 contentEdgeInsets、UICollectionViewFlowLayout.sectionInset 等不支持 directional 的 API) | 用方向化构造:left = isRTL ? trailing : leading,right = isRTL ? leading : trailing |

| frame 手动布局 | 按 x' = 容器宽 - x - 宽 镜像计算;优先改成约束 |


extension UIEdgeInsets {

init(top: CGFloat = 0, leading: CGFloat = 0, bottom: CGFloat = 0, trailing: CGFloat = 0) {

let isRTL = AppLayoutDirection.isRTL

self.init(top: top, left: isRTL ? trailing : leading, bottom: bottom, right: isRTL ? leading : trailing)

}

}

4.2 文本

| 场景 | 规则 |

|---|---|

| 普通文本 | .natural,不要写死 .left/.right |

| 设计要求「居中」 | .center,与方向无关 |

| 富文本 NSAttributedString | NSMutableParagraphStyle.alignment = .natural,并设置 baseWritingDirection = isRTL ? .rightToLeft : .leftToRight |

| 阿语中夹英文 / 数字 | 由 Unicode 双向算法自动处理;顺序错乱时,在片段两侧插入方向标记:\u{200F}(RLM,按 RTL 处理)或 \u{200E}(LRM,按 LTR 处理) |

| 必须保持 LTR 的内容:时间 00:12、价格、手机号、验证码、集数 EP.12、版本号 | 单独 Label 设 semanticContentAttribute = .forceLeftToRight + textAlignment 按布局需要设置;或用 LRM 包裹 |

| 拼接文案 | 用多语言模板占位符(如 "%@ 集" → 由翻译决定顺序),禁止在代码里按 LTR 顺序拼接字符串片段