LTR 到 RTL 在 objective c 中使用相同的 xib

LTR To RTL Using same xib in objective c

我一直坚持一点,我想开发一个同时支持 LTR 和 RTL 的应用程序,但我不想为 LTR 和 RTL 创建两个 .xib。

因此,任何人都可以使用相同的 xib 向我提供从 LTR 到 RTL 的任何可用方法。

提前致谢。

无需创建 2 个 xib 文件即可使 LTR 和 RTL 正常工作。

基本上你需要:

  1. 设置前导尾随约束
  2. 如果 iPhone 语言是 RTL,前导现在将充当尾随,尾随将充当前导(因此无需创建 2 个 xib 文件,您的视图元素将被翻转)
  3. 在某些情况下,您可能希望 force RTL/LTR images using imageFlippedForRightToLeftLayoutDirection()

    image = [image imageFlippedForRightToLeftLayoutDirection];
    

    检查当前语言时使用:

    BOOL isLeftToRight = [UIView userInterfaceLayoutDirectionForSemanticContentAttribute:[[self view] semanticContentAttribute]] == UISemanticContentAttributeForceLeftToRight;
    
  4. 记住每个UIView都有semanticContentAttribute that can be set to force RTL UISemanticContentAttributeForceRightToLeft。即:

    [view setSemanticContentAttribute: UISemanticContentAttributeForceRightToLeft];
    
  5. 当使用 textFields 时,请使用以下对齐方式,这样每个 RTL/LTR 都可以正常工作:

您可以通过更改语义来做到这一点

   self.anyView.semanticContentAttribute = .forceLeftToRight

   self.anyView.semanticContentAttribute = .forceRightToLeft

在objective-c

 self.anyView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;

 self.anyView.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

另请参阅此演示:RTLDemo

下面是我的一些方法。

  • 首先,验证布局方向(你可以为此创建一个宏)

    [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft

  • 将 IBOutlet 用于 NSLayoutConstraint 并设置 constant/priority 以移动您的 UI 元素

  • 对于标签,使用 NSTextAlignmentNatural 在 LTR 中左对齐,对于 RTL 将照顾系统。否则你必须自己设置它们的文本对齐方式
  • 对于某些相关图片,您必须水平翻转它们
  • 对于任何类型的绘图(在 drawRect 或图层中)你应该注意

这些对我很重要,但还有更多。发挥创意