简体   繁体   English

如何将图片放在按钮的右侧,文本向左对齐

[英]How do I put the image on the right side of the button and the text align left

I have a UIButton with image and text. 我有一个带有图像和文本的UIButton How do I align the image to the right, and align the text to the left? 如何将图像向右对齐,并将文本向左对齐?

@IBDesignable class RightImageButton: UIButton {

    @IBInspectable var alignImageToRight: Bool = true

    override func layoutSubviews() {
        super.layoutSubviews()
        if alignImageToRight {
            if let imageView = self.imageView, let titleLabel = self.titleLabel {
                let imageWidth = imageView.frame.size.width
                var imageFrame: CGRect = imageView.frame;
                var labelFrame: CGRect = titleLabel.frame;
                labelFrame.origin.x = self.titleEdgeInsets.left + self.contentEdgeInsets.left
                imageFrame.origin.x = frame.width - self.imageEdgeInsets.right - self.contentEdgeInsets.right - imageWidth
                imageView.frame = imageFrame;
                titleLabel.frame = labelFrame;
            }
        }
    }

}

Use below two methods 使用以下两种方法

button.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0.0)
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0.0)

Pass value for left and right and adjust it. 传递左右的值并进行调整。 Set offset as per you requirement. 根据您的要求设置偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将图像放在 UIButton 中文本的右侧? - How do I put the image on the right side of the text in a UIButton? 可以左对齐按钮文本,但右对齐背景或按钮图像吗? - Possible to left align button text but right align background or button image? 如何在UIViewController的左侧和右侧放置边框 - How do I put a border on the left and right side of my UIViewController 如何在UIButton的左侧和右侧放置文本 - How to put text on left and right side of UIButton 如何使用Swift以编程方式在UIButton中向左对齐文本和向右对齐图像? - How do I align text to the left and an image to the right in a UIButton programmatically using Swift? 如何将MPMediaPickerController取消按钮放在左侧? - How can I put the MPMediaPickerController cancel button on left side? SwiftUI - 如何将多行文本对齐到左侧? - SwiftUI - how to align multiline Text to left side? 如何检测iPhone屏幕右侧或左侧是否有水龙头? - How do I detect if there is a tap on right or left side of the iPhone screen? 如何将图像与 HStack 中的文本对齐? - How do i align an image with text in an HStack? 自动布局:并排对齐两个按钮,当没有图像时移至右侧 - Autolayout : Align two button side by side and move to right side when there is no image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM