Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button with Image and a label on bottom

I want to make a button which has an icon and a label on the bottom of it so like this:

enter image description here

What would be the best way to do this? Is it possible to drag an image view object and a label into a button?

like image 929
Sinan Samet Avatar asked Dec 04 '22 03:12

Sinan Samet


1 Answers

By code you can do it like this Swift 4.2 compatible code is here , you just need to call function

  public extension UIButton
  {

    func alignTextBelow(spacing: CGFloat = 6.0)
    {
        if let image = self.imageView?.image
        {
            let imageSize: CGSize = image.size
            self.titleEdgeInsets = UIEdgeInsets(top: spacing, left: -imageSize.width, bottom: -(imageSize.height), right: 0.0)
            let labelString = NSString(string: self.titleLabel!.text!)
            let titleSize = labelString.size(withAttributes: [NSAttributedString.Key.font: self.titleLabel!.font])
            self.imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0.0, bottom: 0.0, right: -titleSize.width)
        }
    }
}
like image 64
Shahzaib Maqbool Avatar answered Dec 23 '22 19:12

Shahzaib Maqbool