You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.0 KiB

//
// UIButton-Extensions.swift
// HPlusFit
//
// Created by lemo. on 2019/9/15.
// Copyright © 2019 lemo. All rights reserved.
//
import Foundation
extension UIButton {
///
///
/// - Parameters:
/// - type: 0: 1: 2: 3
/// - spacing: ,线
func changgeImageAndTitleEdgeInsets(type: Int, spacing: CGFloat) {
switch type {
case 1:
if let titleLabel = titleLabel, let image = imageView?.image {
titleLabel.sizeToFit()
let labelWidth = titleLabel.width + spacing
let imageWidth = image.size.width
imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth, bottom: 0, right: -labelWidth)
titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth, bottom: 0, right: imageWidth)
}
default:
break
}
}
/// ()
static func barButtonItem(_ normalImageName: String, _ highImageName: String?, _ target: Any?, _ action: Selector) -> UIButton {
let button = UIButton(type: .custom)
let normalImage = UIImage(named: normalImageName)
button.setImage(normalImage, for: .normal)
button.frame.size = (normalImage?.size)!
if highImageName != nil {
button.setImage(UIImage(named: highImageName!), for: .highlighted)
}
button.addTarget(target, action: action, for: .touchUpInside)
return button
}
/// ()
static func barButtonItem(_ title: String?, _ target: Any?, _ action: Selector) -> UIButton {
let button = UIButton(type: .custom)
// button.titleLabel?.font = kNavBarItemFont
// button.setTitleColor(kNavBarItemTextColor, for: .normal)
button.setTitle(title, for: .normal)
button.addTarget(target, action: action, for: .touchUpInside)
button.sizeToFit()
return button
}
}