Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storyboard positioning text below image inside a button

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:

enter image description here

What I am getting now is this: enter image description here

like image 679
sunshine Avatar asked Jun 20 '16 05:06

sunshine


2 Answers

If you are looking for output like this

Button Image with Text

1) Select button and go to Attribute Inspector in your storyboard to get this result of Button size 50*50

enter image description here

2) Now set Title Insets of Button

enter image description here

like image 131
Sourabh Sharma Avatar answered Sep 21 '22 01:09

Sourabh Sharma


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.

like image 25
Luca Davanzo Avatar answered Sep 23 '22 01:09

Luca Davanzo