UIButton+CNLiveExtensions.swift
5.02 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// UIButton+CNLiveExtension.swift
// metaCode
//
// Created by open on 2023/6/7.
//
import Foundation
enum ButtonImageAndTitlePossitionStyle {
case left
case right
case top
case bottom
}
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
}
/// 设置图片文字方向并设置间隔
/// - Parameters:
/// - padding: 间隔
/// - style: 图片所在位置
func setupButtonImageAndTitlePossitionWith(padding: CGFloat, style: ButtonImageAndTitlePossitionStyle){
let imageRect: CGRect = self.imageView?.frame ?? CGRect.init()
let titleRect: CGRect = self.titleLabel?.frame ?? CGRect.init()
let selfWidth: CGFloat = self.frame.size.width
let selfHeight: CGFloat = self.frame.size.height
let totalHeight = titleRect.size.height + padding + imageRect.size.height
switch style {
case .left:
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: padding / 2, bottom: 0, right: -padding / 2)
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -padding / 2, bottom: 0, right: padding / 2)
case .right:
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageRect.size.width + padding/2), bottom: 0, right: (imageRect.size.width + padding/2))
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: (titleRect.size.width + padding / 2), bottom: 0, right: -(titleRect.size.width + padding/2))
case .top :
self.titleEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 + imageRect.size.height + padding - titleRect.origin.y), left: (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2, bottom: -((selfHeight - totalHeight) / 2 + imageRect.size.height + padding - titleRect.origin.y), right: -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2)
self.imageEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 - imageRect.origin.y), left: (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2), bottom: -((selfHeight - totalHeight) / 2 - imageRect.origin.y), right: -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2))
case .bottom:
self.titleEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 - titleRect.origin.y), left: (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2, bottom: -((selfHeight - totalHeight) / 2 - titleRect.origin.y), right: -(selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2)
self.imageEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 + titleRect.size.height + padding - imageRect.origin.y), left: (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2), bottom: -((selfHeight - totalHeight) / 2 + titleRect.size.height + padding - imageRect.origin.y), right: -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2))
}
}
}