I am able to set the image and text for button. But it is aligned horizontally. I want the image and text to be aligned vertically inside the button. i.e., text below the image
How to make these changes using storyboard?
What I want is this:
What I am getting now is this:
If you are looking for output like this
1) Select button and go to Attribute Inspector in your storyboard to get this result of Button size 50*50
2) Now set Title Insets of Button
I've written an extension that do the works for you, Swift 4 compatible.
public extension UIButton { func alignTextBelow(spacing: CGFloat = 6.0) { if let image = self.imageView?.image { let imageSize: CGSize = image.size self.titleEdgeInsets = UIEdgeInsetsMake(spacing, -imageSize.width, -(imageSize.height), 0.0) let labelString = NSString(string: self.titleLabel!.text!) let titleSize = labelString.size(withAttributes: [NSAttributedStringKey.font: self.titleLabel!.font]) self.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + spacing), 0.0, 0.0, -titleSize.width) } } }
Where spacing
is the distance between image and text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With