宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
//  UIButtonExtension.swift
//  THProject
//
//  Created by alvin_y on 2018/4/8.
//  Copyright © 2018年 yang-wang. All rights reserved.
//
 
import Foundation
import Foundation
import UIKit
 
private var key: Void?
 
 
/// 取消高亮状态按钮
class ButtonHighlightedNormal: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override var isHighlighted: Bool{
        get {
            return false
        }
        set {
            super.isHighlighted = false
        }
    }
}
 
 
extension UIButton {
    
    // 图片文字
    enum ImagePosition {
        case left
        case right
        case top
        case bottom
    }
    
    // 渐变方式
    enum GradientType {
        case topToBottom // 上到下
        case leftToRight // 左到右
        case upleftTolowRight // 左上到右下
        case uprightTolowLeft // 右上到左下
    }
    
    /// 利用 runtime 添加一个 indexPath 属性
    var indexPath: IndexPath? {
        get {
            return objc_getAssociatedObject(self, &key) as? IndexPath
        }
        set(newValue) {
            objc_setAssociatedObject(self, &key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    /// 调整图片位置,返回调整后所需要的size
    /// 调用本方法前,请先确保imageView和titleLabel有值。
    @discardableResult
    func adjustImage(position: ImagePosition, spacing: CGFloat) -> CGSize {
        guard imageView != nil && titleLabel != nil else {
            return CGSize.zero
        }
        let imageSize = self.imageView!.intrinsicContentSize
        let titleSize = self.titleLabel!.intrinsicContentSize
        
        // 布局
        switch (position) {
        case .left:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing / 2, bottom: 0, right: spacing / 2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2, bottom: 0, right: -spacing / 2)
            contentEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2, bottom: 0, right: spacing / 2)
        case .right:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: (titleSize.width + spacing / 2), bottom: 0, right: -(titleSize.width + spacing / 2))
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageSize.width + spacing / 2), bottom: 0, right: (imageSize.width + spacing / 2))
            contentEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2, bottom: 0, right: spacing / 2);
        case .top, .bottom:
            let imageOffsetX = (imageSize.width + titleSize.width) / 2 - imageSize.width / 2
            let imageOffsetY = imageSize.height / 2 + spacing / 2
            let titleOffsetX = (imageSize.width + titleSize.width / 2) - (imageSize.width + titleSize.width) / 2
            let titleOffsetY = titleSize.height / 2 + spacing / 2
            let changedWidth = titleSize.width + imageSize.width - max(titleSize.width, imageSize.width)
            let changedHeight = titleSize.height + imageSize.height + spacing - max(imageSize.height, imageSize.height)
            
            if position == .top {
                imageEdgeInsets = UIEdgeInsets(top: -imageOffsetY, left: imageOffsetX, bottom: imageOffsetY, right: -imageOffsetX)
                titleEdgeInsets = UIEdgeInsets(top: titleOffsetY, left: -titleOffsetX, bottom: -titleOffsetY, right: titleOffsetX)
                self.contentEdgeInsets = UIEdgeInsets(top: imageOffsetY, left: -changedWidth / 2, bottom: changedHeight - imageOffsetY, right: -changedWidth / 2);
            } else {
                imageEdgeInsets = UIEdgeInsets(top: imageOffsetY, left: imageOffsetX, bottom: -imageOffsetY, right: -imageOffsetX)
                titleEdgeInsets = UIEdgeInsets(top: -titleOffsetY, left: -titleOffsetX, bottom: titleOffsetY, right: titleOffsetX)
                self.contentEdgeInsets = UIEdgeInsets(top: changedHeight - imageOffsetY, left: -changedWidth / 2, bottom: imageOffsetY, right: -changedWidth / 2);
            }
        }
        
        return self.intrinsicContentSize
    }
    
    
    
    
    ///MARK: -创建一个mormal btn
    class func yy_creatNormal(title:String, fontSize:CGFloat, textColor:UIColor) -> UIButton {
        return yy_creat(normalTitle: title, selectedTitle: nil, normalTextColor: textColor, selectedTextColor: nil, fontSize: fontSize)
    }
    
    class func yy_creat(normalTitle:String, selectedTitle:String?, normalTextColor:UIColor, selectedTextColor:UIColor?, fontSize:CGFloat) -> UIButton {
        let btn = UIButton.init(type: .custom)
        btn.setTitle(normalTitle, for: .normal)
        btn.setTitleColor(normalTextColor, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        if (selectedTitle != nil) {
            btn.setTitle(selectedTitle, for: .selected)
            if (selectedTextColor != nil) {
                btn.setTitleColor(selectedTextColor, for: .selected)
            } else {
                btn.setTitleColor(normalTextColor, for: .selected)
            }
        }
        
        return btn
    }
    
    //MARK: - 添加一个渐变背景
    func yy_addGradient(colors:[UIColor],gradientType:GradientType) {
        var otherGradient = UIImage.GradientType.topToBottom
        switch gradientType {
        case .leftToRight:
            otherGradient = UIImage.GradientType.leftToRight
            break
        case .topToBottom:
            otherGradient = UIImage.GradientType.topToBottom
            break
        case .uprightTolowLeft:
            otherGradient = UIImage.GradientType.uprightTolowLeft
            break
        case .upleftTolowRight:
            otherGradient = UIImage.GradientType.upleftTolowRight
            break
        }
        let image = UIImage.yy_creat(size: self.bounds.size, colors: colors, gradientType: otherGradient)
        self.setBackgroundImage(image, for: .normal)
    }
    
}
//MARK: - 自由调整图标按钮中的图标和文字位置
extension UIButton {
    
    @objc func set(image anImage: UIImage?, title: String,
                   titlePosition: UIView.ContentMode, additionalSpacing: CGFloat, state: UIControl.State){
        self.imageView?.contentMode = .center
        self.setImage(anImage, for: state)
        
        positionLabelRespectToImage(title: title, position: titlePosition, spacing: additionalSpacing)
        
        self.titleLabel?.contentMode = .center
        self.setTitle(title, for: state)
    }
    
    private func positionLabelRespectToImage(title: String, position: UIView.ContentMode,
                                             spacing: CGFloat) {
        let imageSize = self.imageRect(forContentRect: self.frame)
        let titleFont = self.titleLabel?.font!
        let titleSize = title.size(withAttributes: [NSAttributedString.Key.font: titleFont!])
        var titleInsets: UIEdgeInsets
        var imageInsets: UIEdgeInsets
        
        switch (position){
        case .top:
            titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
        case .bottom:
            titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
        case .left:
            titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0,
                                       right: -(titleSize.width * 2 + spacing))
        case .right:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        default:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        
        self.titleEdgeInsets = titleInsets
        self.imageEdgeInsets = imageInsets
    }
}