我想使用NSDirectionalEdgeInsets
toUIButton
和contentEdgeInsets
. titleEdgeInsets
那可能吗?
背景
出于本地化目的,在设置组件的插图时,iOS 应用程序可能需要同时适应从左到右和从右到左的语言。
Apple 推出NSDirectionalEdgeInsets
了 iOS11,不幸的是,这仅适用于少数属性,例如directionalLayoutMargins
, 不推荐UIEdgeInsets
用于layoutMargins
.
NSDirectionalEdgeInsets
使用前导和尾随修饰符而不是其对应项使用的左右修饰符来尊重界面布局方向。UIEdgeInsets
当前不正确的解决方案
对每个修改过的属性的每个按钮/视图使用以下代码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)
}