iOS开发,修改UIButton按钮图片和标题位置

  • 代码如下
extension UIButton {
        /// 图片位置
        enum ImagePosition {
        case left 
        case right 
        case top
        case bottom
    }

    /// 调整按钮图片和文字的位置
    func setImagePosition(position: ImagePosition, spacing: CGFloat) {
        setTitle(currentTitle, for: .normal)
        setImage(currentImage, for: .normal)
        
        let imageWidth = imageView?.image?.size.width ?? 0
        let imageHeight = imageView?.image?.size.height ?? 0
        let labelWidth = titleLabel?.text?.size(withAttributes: [.font: titleLabel?.font ?? UIFont.systemFont(ofSize: 0)]).width ?? 0
        let labelHeight = titleLabel?.text?.size(withAttributes: [.font: titleLabel?.font ?? UIFont.systemFont(ofSize: 0)]).height ?? 0
        
        let imageOffsetX = (imageWidth + labelWidth) / 2.0 - imageWidth / 2.0 // image中心移动的x距离
        let imageOffsetY = imageHeight / 2.0 + spacing / 2.0 // image中心移动的y距离
        let labelOffsetX = (imageWidth + labelWidth / 2.0) - (imageWidth + labelWidth) / 2.0 // label中心移动的x距离
        let labelOffsetY = labelHeight / 2.0 + spacing / 2.0 // label中心移动的y距离
        
        let tempWidth = max(labelWidth, imageWidth)
        let changedWidth = labelWidth + imageWidth - tempWidth
        let tempHeight = max(labelHeight, imageHeight)
        let changedHeight = labelHeight + imageHeight + spacing - tempHeight
        
        switch position {
        case .left:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
            contentEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: spacing/2)
        case .right:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth + spacing/2, bottom: 0, right: -(labelWidth + spacing/2))
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageWidth + spacing/2), bottom: 0, right: imageWidth + spacing/2)
            contentEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom:0, right: spacing/2)
        case .top:
            imageEdgeInsets = UIEdgeInsets(top: -imageOffsetY, left: imageOffsetX, bottom: imageOffsetY, right: -imageOffsetX)
            titleEdgeInsets = UIEdgeInsets(top: labelOffsetY, left: -labelOffsetX, bottom: -labelOffsetY, right: labelOffsetX)
            contentEdgeInsets = UIEdgeInsets(top: imageOffsetY, left: -changedWidth/2, bottom: changedHeight-imageOffsetY, right: -changedWidth/2)
        case .bottom:
            imageEdgeInsets = UIEdgeInsets(top: imageOffsetY, left: imageOffsetX, bottom: -imageOffsetY, right: -imageOffsetX)
            titleEdgeInsets = UIEdgeInsets(top: -labelOffsetY, left: -labelOffsetX, bottom: labelOffsetY, right: labelOffsetX)
            contentEdgeInsets = UIEdgeInsets(top: changedHeight-imageOffsetY, left: -changedWidth/2, bottom: imageOffsetY, right: -changedWidth/2)
        }
        
    }
}
  • 使用方式如下
let btn = UIButton(type: .custom)
btn.setTitle("标题", for: .normal)
btn.setImage(UIImage(named: "xxx"), for: .normal)
btn.setImagePosition(position: .right, spacing: 4)

如果修改了标题内容,需要重新调用一遍
setImagePosition

btn.setTitle("修改标题", for: .normal)
btn.setImagePosition(position: .right, spacing: 4)

你可能感兴趣的:(iOS开发,修改UIButton按钮图片和标题位置)