UIButton+CNLiveExtension.swift
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// UIButton+CNLiveExtension.swift
// metaCode
//
// Created by open on 2023/6/7.
//
import Foundation
extension UIButton {
/// 根据图片生成UIButton
static func imageItem(imageName:String,target:AnyObject,action:Selector) -> UIButton {
return customItem(title: "", titleColor: UIColor.white, imageName: imageName, target: target, action: action,contentHorizontalAlignment: .center)
}
/// 根据文字生成UIButton
static func textItem(title:String,titleColor:UIColor,target:AnyObject,action:Selector) -> UIButton {
return customItem(title: title, titleColor: titleColor, imageName: "", target: target, action: action,contentHorizontalAlignment: .center)
}
/// 返回按钮 带箭头的
static func backItem(imageName:String,target:AnyObject,action:Selector) -> UIButton {
return customItem(title: "", titleColor: UIColor.white, imageName: imageName, target: target, action: action,contentHorizontalAlignment: .left,isBack: true)
}
/// 快速初始化一个UIButton,内部是按钮
static func customItem(title:String,titleColor:UIColor,imageName:String,target:AnyObject,action:Selector,
contentHorizontalAlignment:UIControl.ContentHorizontalAlignment,isBack:Bool=false) -> UIButton {
let button = UIButton()
if (title.count>0) {
button.setTitle(title, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
}
if (imageName.count>0) {
button.setImage(UIImage(named: imageName), for: .normal)
button.setImage(UIImage(named: imageName), for: .highlighted)
}
button.setTitleColor(titleColor, for: .normal)
button.setTitleColor(titleColor.withAlphaComponent(0.5), for: .highlighted)
button.setTitleColor(titleColor.withAlphaComponent(0.5), for: .disabled)
button.addTarget(target, action: action, for: .touchUpInside)
if (isBack) {
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
} else {
button.sizeToFit()
}
button.contentHorizontalAlignment = contentHorizontalAlignment;
return button
}
}