简体   繁体   English

UIButton 标题对齐在 RTL 中没有改变到正确的方向

[英]UIButton title alignment not changing to right direction in RTL

I am having an application in both LTR and RTL languages.我有一个 LTR 和 RTL 语言的应用程序。 Everything is fine but the UIButton title alignment is not changing to right though the UIButton flips itself.一切都很好,但 UIButton 标题对齐并没有改变,尽管 UIButton 自己翻转。 Here is the screenshot:这是屏幕截图: RTL 中的 UIButton

给定的对齐方式

Constraints given for default language english:为默认语言英语给出的限制:

为默认语言英语给出的限制:

What is the issue and why it happens?这是什么问题,为什么会发生? Do we need to set the title programatically aligned to right?我们是否需要以编程方式将标题设置为右对齐?

Use right alignment from storyboard as使用故事板中的右对齐作为

在此处输入图片说明

or from programmatically as或以编程方式作为

buttonShowOnMap.contentHorizontalAlignment = .left//For left alignment

buttonShowOnMap.contentHorizontalAlignment = .right//For right alignment

Accepted answer did not help me.接受的答案对我没有帮助。 But following snippet worked to support both RTL and LTR languages automatically:但是以下代码段可以自动支持 RTL 和 LTR 语言:

// makes button content support RTL <-> LTR
button.contentHorizontalAlignment = .leading

// makes titleLabel support RTL <-> LTR
button.titleLabel?.textAlignment = .natural 

No idea why happening but you must subclass UIButton so that you can override alignmentRectInsets .不知道为什么会发生,但您必须继承 UIButton 以便您可以覆盖alignmentRectInsets

override func alignmentRectInsets() -> UIEdgeInsets {
    var insets: UIEdgeInsets
    if (Left_Side_Button) {
        insets = UIEdgeInsetsMake(0, 7.0, 0, 0)
    }
    else {
        // Right Button
        insets = UIEdgeInsetsMake(0, 0, 0, 7.0)
    }
    return insets
}

And achieve it by above code.并通过上述代码实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM