使用 UIWebView 编辑 HTML 内容时输入语言发生变化(LTR / RTL)时如何正确调整文本对齐方式?

How to properly adjust text alignment when input language changes (LTR / RTL) while editing HTML contents with UIWebView?

对于 UITextField 和 UITextView 这似乎是自动的。当文本为空时,如果将键盘更改为 "ar" 和 "he-IL" 等 RLT 语言,光标将移至最右侧的位置。当您将键盘更改为其他语言时,光标会移动到最左侧的位置。对于 UIWebView,无论选择何种输入语言(设备主要语言为 LTR),光标始终位于最左侧的位置。

我可以通过设置 dir 属性来强制 RTL 或 LRT 文本对齐 HTML 以检测键盘输入语言的变化,例如:

[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.dir='rtl'"]

[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.dir='ltr'"]

这很有效,但似乎适用于整个内容。 Apple 本机邮件做得更好,只有当前文本 line/paragraph 的对齐方式受此影响。不过,我不确定 Apple 原生邮件是否使用 UIWebView 实现。

我正在寻找 suggestions/hints 以便我可以模仿 Apple 本机邮件 HTML 编辑行为(具有 RTL 语言支持)。

您可以将 CSS 属性 "direction" 应用于要更改的页面中的特定 html 元素。 至于知道用户何时在 iOS 键盘中切换语言,您可以收听 UITextInputCurrentInputModeDidChangeNotification 通知

这将为您提供当前语言:

[UITextInputMode currentInputMode].primaryLanguage