iOS ObjectiveC:支持从右到左

iOS ObjectiveC: Support Right To Left

为了在我的应用中支持从右到左,我使用了以下说明:

[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];

[[UIStackView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];

[UITextView appearance].textAlignment = NSTextAlignmentRight;

但是我有很多问题: - 如果我需要在当前视图中应用新对齐方式,我不明白如何刷新 UI。 - 在 collection 视图中,即使我使用:

对齐也不会改变
[[UICollectionView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];

所以我决定使用:

self.sectionsCollectionView.transform = CGAffineTransformMakeScale(-1, 1);

和:

cell.transform = CGAffineTransformMakeScale(-1, 1);

它可以工作,但是如果我按下另一个视图并在我弹出到带有 collection 视图的视图后,它会再次向左对齐,直到我进入 header 部分,之后点击它到右边。

根据 Apple 的文档,

By default, text alignment in iOS is natural; in OS X, it’s left. Using natural text alignment aligns text on the left in a left-to-right language, and automatically mirrors the alignment for right-to-left languages. For example, if you set the alignment of an NSMutableParagraphStyle object using the setAlignment: method, pass NSNaturalTextAlignment as the parameter.

[[(NSMutableParagraphStyle *)paraStyle setAlignment:NSNaturalTextAlignment];

However, if you want to align a control to the right in a left-to-right language (and to the left in a right-to-left language), get the layout direction, as described in Getting the Layout Direction, and set the alignment to NSLeftTextAlignment for a right-to-left language.

详情请参考Supporting Right-to-Left Languages