Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vladimir-anisimov/81b5b9ca77c918216fd5089254fcbb43 to your computer and use it in GitHub Desktop.
Save vladimir-anisimov/81b5b9ca77c918216fd5089254fcbb43 to your computer and use it in GitHub Desktop.
import UIKit
extension UIButton {
func centerTextAndImage(spacing: CGFloat) {
let insetAmount = spacing / 2
let writingDirection = UIApplication.shared.userInterfaceLayoutDirection
let factor: CGFloat = writingDirection == .leftToRight ? 1 : -1
self.imageEdgeInsets = UIEdgeInsets(top: 0,
left: -insetAmount*factor,
bottom: 0,
right: insetAmount*factor)
self.titleEdgeInsets = UIEdgeInsets(top: 0,
left: insetAmount*factor,
bottom: 0,
right: -insetAmount*factor)
self.contentEdgeInsets = UIEdgeInsets(top: 0,
left: insetAmount,
bottom: 0,
right: insetAmount)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment