一) 使用场景
- 多用于默认UIButton图片与标题位置调整(默认为图片在左,标题在右)
- 扩展于UIButton,使用灵活;
-
效果如下;
- ①图片在上方,文字在下方
- ②图片在左侧,文字在右侧
效果图
二) 使用方法
- 扩展于UIButton,仅一行代码即可重置图片与标题的相对位置(间距可设置,默认为0个像素)
button.adjustImageTitlePosition(.top, spacing: 6)
三) 源码分享
//
// CCButton.swift
// HelloSwift
//
// Created by a51095 on 2021/7/15.
//
extension UIButton {
/// 逆时针方向🔄
enum Position { case top, left, bottom, right }
/// 重置图片image与标题title位置(默认间距为0)
func adjustImageTitlePosition(_ position: Position, spacing: CGFloat = 0 ) {
self.sizeToFit()
let imageWidth = self.imageView?.image?.size.width
let imageHeight = self.imageView?.image?.size.height
let labelWidth = self.titleLabel?.frame.size.width
let labelHeight = self.titleLabel?.frame.size.height
switch position {
case .top:
imageEdgeInsets = UIEdgeInsets(top: -labelHeight! - spacing / 2, left: 0, bottom: 0, right: -labelWidth!)
titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!, bottom: -imageHeight! - spacing / 2, right: 0)
break
case .left:
imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing / 2, bottom: 0, right: 0)
titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing * 1.5, bottom: 0, right: 0)
break
case .bottom:
imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight! - spacing / 2, right: -labelWidth!)
titleEdgeInsets = UIEdgeInsets(top: -imageHeight! - spacing / 2, left: -imageWidth!, bottom: 0, right: 0)
break
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)
break
}
}
}
网友评论