Skip to content

Instantly share code, notes, and snippets.

@brownsoo
Created January 12, 2024 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brownsoo/d13d15c934783761a2ac5a9420e1012f to your computer and use it in GitHub Desktop.
Save brownsoo/d13d15c934783761a2ac5a9420e1012f to your computer and use it in GitHub Desktop.
UIButton image vertical aligned with text
// from; https://developer-eungb.tistory.com/30
extension UIButton {
func alignTextBelow(spacing: CGFloat = 4.0) {
guard let image = self.imageView?.image else {
return
}
guard let titleLabel = self.titleLabel else {
return
}
guard let titleText = titleLabel.text else {
return
}
let titleSize = titleText.size(withAttributes: [
NSAttributedString.Key.font: titleLabel.font as Any
])
titleEdgeInsets = UIEdgeInsets(top: spacing, left: -image.size.width, bottom: -image.size.height, right: 0)
imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0, bottom: 0, right: -titleSize.width)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment