storyboard positioning text below image inside a button

前端 未结 8 1106
野趣味
野趣味 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:42

    Thank you @rbiard. Another workaround when you are developing a multilingual mobile app and when using UISemanticContentAttribute (right to left and left to right). This should work:

    func alignTextBelow(spacing: CGFloat = 10.0) {
    
        //when the selected language is English
        if L102Language.currentAppleLanguage() == "en" {
            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)
        } else {
            //When selecting a right to left language. For example, Arabic
            guard let image = imageView?.image, let label = titleLabel,
                let string = label.text else { return }
    
            titleEdgeInsets = UIEdgeInsets(top: spacing, left: 0, bottom: -image.size.height, right: -image.size.width)
            let titleSize = string.size(withAttributes: [NSAttributedString.Key.font: label.font])
            imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: -titleSize.width, bottom: 0.0, right: 0)
        }
    }
    

提交回复
热议问题