Swift UIButton(按钮)图片在上,文字在下

释放双眼,带上耳机,听听看~!
//MARK: -定义button相对label的位置
enum RGButtonImagePosition {
case top          //图片在上,文字在下,垂直居中对齐
case bottom       //图片在下,文字在上,垂直居中对齐
case left         //图片在左,文字在右,水平居中对齐
case right        //图片在右,文字在左,水平居中对齐
}
/// - Description 设置Button图片的位置
/// - Parameters:
///   - style: 图片位置
///   - spacing: 按钮图片与文字之间的间隔
func imagePosition(style: RGButtonImagePosition, spacing: CGFloat) {
//得到imageView和titleLabel的宽高
let imageWidth = self.imageView?.frame.size.width
let imageHeight = self.imageView?.frame.size.height
var labelWidth: CGFloat! = 0.0
var labelHeight: CGFloat! = 0.0
labelWidth = self.titleLabel?.intrinsicContentSize.width
labelHeight = self.titleLabel?.intrinsicContentSize.height
//初始化imageEdgeInsets和labelEdgeInsets
var imageEdgeInsets = UIEdgeInsets.zero
var labelEdgeInsets = UIEdgeInsets.zero
//根据style和space得到imageEdgeInsets和labelEdgeInsets的值
switch style {
case .top:
//上 左 下 右
imageEdgeInsets = UIEdgeInsets(top: -labelHeight-spacing/2, left: 0, bottom: 0, right: -labelWidth)
labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!, bottom: -imageHeight!-spacing/2, right: 0)
break;
case .left:
imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing)
labelEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
break;
case .bottom:
imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight!-spacing/2, right: -labelWidth)
labelEdgeInsets = 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)
labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!-spacing/2, bottom: 0, right: imageWidth!+spacing/2)
break;
}
self.titleEdgeInsets = labelEdgeInsets
self.imageEdgeInsets = imageEdgeInsets
}

人已赞赏
iOS文章

C 语言常用的函数(ceil-向上取整,floor-向下取整,round-四舍五入)

2020-3-28 22:32:08

iOS文章

iOS 常用的排序算法-OC版//快速排序 - (void)quickSort:(NSMutableArray *)arr start:(int)start end:(int)end { //

2020-3-28 23:47:10

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索