自定义UIButton,调整图片和文字位置

     自定义UIButton只需要在layoutSubviews方法中根据想要的布局位置重新计算UIButton中的ImageView和Label位置即可。但是有一点需要注意,计算出来的imgeView和Label的总宽度和总高度不能大于UIButton自身的大小,否则点击超过UIButton自身大小区域时会不响应事件。

    Swift大致代码如下:

       1.先定义ImageView和Lable之间的间距和位置关系

    private let spacing: CGFloat
    enum SSImagePosition{
        case top
        case left
        case bottom
        case right
        
    }

     2.重写layoutSubviews

    override func layoutSubviews() {
        super.layoutSubviews()
        if self.bounds.isEmpty {
            return
        }
        let btnSize = self.bounds.size
        let textSize = self.titleLabel?.sizeThatFits(btnSize) ?? CGSize.zero
        let imageSize = self.imageView?.image?.size ?? CGSize.zero
        switch self.position {
        case .top:
            let elementsHeight = textSize.height + imageSize.height + self.spacing
            var imageY: CGFloat = 0
            var textY: CGFloat = btnSize.height - textSize.height
            if elementsHeight <= btnSize.height {
                imageY = (btnSize.height - elementsHeight)/CGFloat(2)
                textY = imageY + imageSize.height + self.spacing
            }
            
            
            self.titleLabel?.frame = CGRect(x: (btnSize.width - textSize.width)/CGFloat(2), y: textY, width: textSize.width, height: textSize.height)
            self.imageView?.frame = CGRect(x:  (btnSize.width - imageSize.width)/CGFloat(2), y: imageY, width: imageSize.width, height: imageSize.height)

        case .left:
            let elementsWidth = textSize.width + imageSize.width + self.spacing
  
            if elementsWidth >= btnSize.width {
                return
            }
            
            let imageX = (btnSize.width - elementsWidth)/CGFloat(2)
            let textX = imageX + imageSize.width + self.spacing

            
            self.titleLabel?.frame = CGRect(x: textX, y: (btnSize.height - textSize.height)/CGFloat(2), width: textSize.width, height: textSize.height)
            self.imageView?.frame = CGRect(x: imageX , y: (btnSize.height - imageSize.height)/CGFloat(2.0), width: imageSize.width, height: imageSize.height)
            
        case .bottom:
            let elementsHeight = textSize.height + imageSize.height + self.spacing
            var imageY: CGFloat =  btnSize.height - imageSize.height

            var textY: CGFloat = 0
            if elementsHeight <= btnSize.height {
                textY = (btnSize.height - elementsHeight)/CGFloat(2)
                imageY = textY + textSize.height + self.spacing

            }
            
            self.titleLabel?.frame = CGRect(x: (btnSize.width - textSize.width)/CGFloat(2), y: textY, width: textSize.width, height: textSize.height)
            self.imageView?.frame = CGRect(x:  (btnSize.width - imageSize.width)/CGFloat(2), y: imageY, width: imageSize.width, height: imageSize.height)
            
        case .right:
            let elementsWidth = textSize.width + imageSize.width + self.spacing
            var imageX: CGFloat = 0
            var textX: CGFloat = btnSize.width - textSize.width
            if elementsWidth <= btnSize.width {
                textX = (btnSize.width - elementsWidth)/CGFloat(2)
                imageX = textX + textSize.width + self.spacing

            }
            
            
            self.titleLabel?.frame = CGRect(x: textX, y: (btnSize.height - textSize.height)/CGFloat(2), width: textSize.width, height: textSize.height)
            self.imageView?.frame = CGRect(x: imageX , y: (btnSize.height - imageSize.height)/CGFloat(2.0), width: imageSize.width, height: imageSize.height)

        }
    }

 

转载于:https://www.cnblogs.com/shengzaiwen/p/10925142.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值