I would like to use NSDirectionalEdgeInsets
to UIButton
's contentEdgeInsets
and titleEdgeInsets
. Is that possible?
Background
For localization purposes, an iOS application may be required to adapt to both Left-To-Right and Right-To-Left languages when setting up a component's insets.
Apple introduced NSDirectionalEdgeInsets
with iOS11, unfortunately this was applied to few properties only like directionalLayoutMargins
, deprecating UIEdgeInsets
which is used for layoutMargins
.
NSDirectionalEdgeInsets
honors interface layout direction using leading and trailing modifiers instead of left and right which is used by its counterpart UIEdgeInsets
.
Current Improper Solution
Using the following code for every button/view with every modified UIEdgeInsets
property is quite cumbersome and prone to errors when making changes:
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)
}