43 lines
1.3 KiB
Swift
43 lines
1.3 KiB
Swift
//
|
|
// gp_btn.swift
|
|
// Hthik
|
|
//
|
|
// Created by 忆海16 on 2024/5/31.
|
|
// Copyright © 2024 Hthik. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
class ButtonModifier {
|
|
|
|
// 设置按钮标题
|
|
static func setTitle(of button: UIButton, to title: String, for state: UIControl.State = .normal) {
|
|
button.setTitle(title, for: state)
|
|
}
|
|
|
|
// 设置按钮标题颜色
|
|
static func setTitleColor(of button: UIButton, to color: UIColor, for state: UIControl.State = .normal) {
|
|
button.setTitleColor(color, for: state)
|
|
}
|
|
|
|
// 设置按钮背景颜色
|
|
static func setBackgroundColor(of button: UIButton, to color: UIColor, for state: UIControl.State = .normal) {
|
|
button.setBackgroundImage(color.image(), for: state)
|
|
}
|
|
|
|
// 添加按钮点击事件
|
|
static func addTarget(to button: UIButton, target: Any?, action: Selector, for event: UIControl.Event = .touchUpInside) {
|
|
button.addTarget(target, action: action, for: event)
|
|
}
|
|
}
|
|
|
|
extension UIColor {
|
|
func image(size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
|
|
return UIGraphicsImageRenderer(size: size).image { rendererContext in
|
|
self.setFill()
|
|
rendererContext.fill(CGRect(origin: .zero, size: size))
|
|
}
|
|
}
|
|
}
|