日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

自定義UIButton,調(diào)整圖片和文字位置

 印度阿三17 2019-05-26

? ? ?自定義UIButton只需要在layoutSubviews方法中根據(jù)想要的布局位置重新計(jì)算UIButton中的ImageView和Label位置即可。但是有一點(diǎn)需要注意,計(jì)算出來的imgeView和Label的總寬度和總高度不能大于UIButton自身的大小,否則點(diǎn)擊超過UIButton自身大小區(qū)域時(shí)會(huì)不響應(yīng)事件。

? ? Swift大致代碼如下:

? ? ? ?1.先定義ImageView和Lable之間的間距和位置關(guān)系

    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)

        }
    }

?

來源:http://www./content-4-205901.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多