storyboard positioning text below image inside a button storyboard positioning text below image inside a button xcode xcode

storyboard positioning text below image inside a button


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


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.


Yes you can do it from storyboard without writing any logic also.

1) Select button and go to Attribute Inspector in your storyboard.

2) Assign Image to the button. (Don't use background Image)

3) Set Title text to that button.

4) Now you need to set edge and Inset so first select image from edge and set Inset as you need and then select title from edge and set inset as per your need.

Hope this helps.