storyboard positioning text below image inside a button

前端 未结 8 1109
野趣味
野趣味 2021-02-01 13:13

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<

相关标签:
8条回答
  • 2021-02-01 13:55

    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

    0 讨论(0)
  • 2021-02-01 13:56

    a refined one

    func alignTextUnderImage(spacing: CGFloat = 6.0) {
        guard let image = imageView?.image, let label = titleLabel,
          let string = label.text else { return }
    
          titleEdgeInsets = UIEdgeInsets(top: spacing, left: -image.size.width, bottom: -image.size.height, right: 0.0)
          let titleSize = string.size(withAttributes: [NSAttributedString.Key.font: label.font])
          imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0.0, bottom: 0.0, right: -titleSize.width)
      }
    
    
    0 讨论(0)
提交回复
热议问题