简体   繁体   English

如何将图像大小和文本之间的比例设置为 UIButton

[英]How to set image size and ratio between text into UIButton

I have a UIButton .我有一个UIButton And finally, I should get this:最后,我应该得到这个:

带有图像和标题的按钮

No matter how I try, I can't get the desired result.无论我如何尝试,我都无法得到想要的结果。 I have read many questions regarding this topic.我已经阅读了很多关于这个主题的问题。 I tried to experiment with titleEdgeInsets and imageEdgeInsets , but it did not help.我尝试使用titleEdgeInsetsimageEdgeInsets进行试验,但没有帮助。 The fact is that when I set the image for the button, it takes up the entire content of the button and the text remains behind.事实是,当我为按钮设置图像时,它占据了按钮的全部内容,而文本则留在后面。 Below how i set the title下面我如何设置标题

let button = UIButton()

button.frame = CGRect(x: 150, y: 300, width: 100, height: 20)
button.backgroundColor = .blue
button.layer.borderColor = UIColor.green.cgColor
button.layer.borderWidth = 2

button.setImage(UIImage(named: "baseline_play_arrow_black_48.png"), for: .normal)
button.imageView?.backgroundColor = .red
button.imageView?.contentMode = .scaleAspectFit

button.setTitle("Play", for: .normal)
button.setTitleColor(.green, for: .normal)
button.titleLabel?.backgroundColor = .yellow

As an experiment I had tried in the playground.作为一个实验,我曾在操场上尝试过。 And code above gave the following result:上面的代码给出了以下结果:

图片 . .

How can i get the desired result?我怎样才能得到想要的结果?

I believe there are a few ways you can achieve this Ahmet, I will go over one idea.我相信有几种方法可以实现这个 Ahmet,我将 go 超过一个想法。

The interesting challenge from your question is that you don't only want to left align the image.您问题的有趣挑战是您不仅要左对齐图像。

You want to:你想要:

  • Left align the image with respect to the button title only仅相对于按钮标题左对齐图像
  • However, the image along with the text should be center aligned as a whole within the UIButton但是,图像和文本应该在 UIButton 中作为一个整体居中对齐

I created the below extension which gets you close to your desired result with some comments to explain my thought process:我创建了以下扩展,它可以让您接近您想要的结果,并附上一些评论来解释我的思考过程:

extension UIButton
{
    func configureWithLeftImage(_ imageName: String)
    {
        // Retrieve the desired image and set it as the button's image
        let buttonImage = UIImage(named: "play_icon")?.withRenderingMode(.alwaysOriginal)
        setImage(buttonImage, for: .normal)
        imageView?.contentMode = .scaleAspectFit
        
        // Align the content inside the UIButton to be left
        contentHorizontalAlignment = .left
        
        // Set or compute the width of the UIImageView within the UIButton
        let imageWidth: CGFloat = imageView!.frame.width
        
        // Specify the padding you want between UIButton and the text
        let contentPadding: CGFloat = 10.0
        
        // Get the width required for your text in the button
        let titleFrame = titleLabel!.intrinsicContentSize.width
        
        // Keep a hold of the button width to make calculations easier
        let buttonWidth = frame.width
        
        // The UIImage and the Text combined should be centered so we need to calculate
        // the x position of the image first.
        let imageXPos = (buttonWidth - (imageWidth + contentPadding + titleFrame)) / 2
        
        // Set the position of the image within the button
        imageEdgeInsets = UIEdgeInsets(top: 0.0,
                                       left: imageXPos,
                                       bottom: 0.0,
                                       right: contentPadding)
    }
}

Then when you want to use it:然后当你想使用它时:

        // Initialize a UIButton and set its frame
        let customButton: UIButton = UIButton(type: .custom)
        customButton.frame = CGRect(x: 100, y: 200, width: 150, height: 40)
        
        // Customize the button as you wish
        customButton.backgroundColor = .white
        customButton.layer.cornerRadius = 5.0
        customButton.setTitleColor(.black, for: .normal)
        customButton.setTitle("смотрите", for: .normal)
        
        // Call the function we just created to configure the button
        customButton.configureWithLeftImage("play_icon")
        
        // Finally add the button to your view
        view.addSubview(customButton)

This is the end result produced which I believe is close to your desired goal:这是产生的最终结果,我认为它接近您的预期目标:

使用左对齐 UIImage 自定义 UIButton

This is the image I used as well inside the UIButton if you want to test it out如果您想测试一下,这也是我在 UIButton 中使用的图像

播放按钮图标

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM