支持 NSDirectionalEdgeInsets 用于 UIButton 的 contenEdgeInsets?

Support NSDirectionalEdgeInsets for UIButton's contenEdgeInsets?

我想使用 NSDirectionalEdgeInsetsUIButtoncontentEdgeInsetstitleEdgeInsets。这可能吗?

背景

出于本地化目的,可能需要 iOS 应用程序在设置组件的插图时适应从左到右和从右到左的语言。

Apple 推出 NSDirectionalEdgeInsets with iOS11, unfortunately this was applied to few properties only like directionalLayoutMargins, deprecating UIEdgeInsets which is used for layoutMargins

NSDirectionalEdgeInsets 使用 leadingtrailing 修饰符而不是 left[=42 来遵循界面布局方向=] 和 right 被其对应 UIEdgeInsets 使用。

当前不正确的解决方案

对每个 button/view 和每个修改后的 UIEdgeInsets 属性 使用以下代码非常麻烦,并且在进行更改时容易出错:

let isRTL: UIUserInterfaceLayoutDirection = // logic to determine language direction 

if isRTL == .leftToRight {
    nextButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
} else {
    nextButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
}

解决方案

import UIKit

extension UIView {
    // Returns `UIEdgeInsets` set using `leading` and `trailing` modifiers adaptive to the language direction
    func getDirectionalUIEdgeInsets(top: CGFloat, leading: CGFloat, bottom: CGFloat, trailing: CGFloat) -> UIEdgeInsets {
        // NOTE: this wil be deprecated when Apple use `NSDirectioanlEdgeInsets` (https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets) for your insets property instead of `UIEdgeInsets`

        if self.userInterfaceLayoutDirection == .leftToRight {
            return UIEdgeInsets(top: top, left: leading, bottom: bottom, right: trailing)
        } else {
            return UIEdgeInsets(top: top, left: trailing, bottom: bottom, right: leading)
        }
    }

    /// Returns text and UI direction based on current view settings
    var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection
    {
        if #available(iOS 9.0, *) {
            return UIView.userInterfaceLayoutDirection(for: self.semanticContentAttribute)
        } else {
            return UIApplication.shared.userInterfaceLayoutDirection
        }
    }
}

用法

nextButton.contentEdgeInsets = nextButton.getDirectionalUIEdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 0)

我在 Whosebug 上寻找答案,但一无所获。因此,我正在分享我的答案。

学分

感谢 David Rysanekthis Whosebug answer userInterfaceLayoutDirection 扩展。