68 lines
2.3 KiB
Swift
68 lines
2.3 KiB
Swift
//
|
|
// UIButtom+.swift
|
|
// yinmeng-ios
|
|
//
|
|
// Created by duoban on 2024/3/6.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
extension UIButton {
|
|
private static var clickRadius = "YinClickRadius"
|
|
|
|
var setBtnClickRadius: UIEdgeInsets? {
|
|
set {
|
|
objc_setAssociatedObject(self, &Self.clickRadius, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
|
|
}
|
|
get {
|
|
return objc_getAssociatedObject(self, &Self.clickRadius) as? UIEdgeInsets ?? UIEdgeInsets.zero
|
|
}
|
|
}
|
|
|
|
/// 重写系统方法修改点击区域
|
|
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
|
guard let inset = setBtnClickRadius else {
|
|
return super.point(inside: point, with: event)
|
|
}
|
|
var bounds = self.bounds
|
|
|
|
let x: CGFloat = -inset.left
|
|
let y: CGFloat = -inset.top
|
|
let width: CGFloat = bounds.width + inset.left + inset.right
|
|
let height: CGFloat = bounds.height + inset.top + inset.bottom
|
|
bounds = CGRect(x: x, y: y, width: width, height: height) // 负值是方法响应范围
|
|
|
|
return bounds.contains(point)
|
|
}
|
|
|
|
static func getCustomBtn(text:String? = nil,selectedText:String? = nil,font:UIFont? = nil,color:UIColor? = nil,selectedColor:UIColor? = nil,image:UIImage? = nil,selectedImage:UIImage? = nil,bgImage:UIImage? = nil) -> UIButton{
|
|
let customBtn = UIButton()
|
|
if let _text = text{
|
|
customBtn.setTitle(text, for: .normal)
|
|
}
|
|
if let _selectedText = selectedText{
|
|
customBtn.setTitle(selectedText, for: .selected)
|
|
}
|
|
if let _font = font{
|
|
customBtn.titleLabel?.font = font
|
|
}
|
|
if let _color = color{
|
|
customBtn.setTitleColor(color, for: .normal)
|
|
}
|
|
if let _selectedColor = selectedColor{
|
|
customBtn.setTitleColor(selectedColor, for: .selected)
|
|
}
|
|
if let _image = image{
|
|
customBtn.setImage(_image, for: .normal)
|
|
}
|
|
if let _selectedImage = selectedImage{
|
|
customBtn.setImage(_selectedImage, for: .selected)
|
|
}
|
|
if let _bgImage = bgImage{
|
|
customBtn.setBackgroundImage(_bgImage, for: .normal)
|
|
}
|
|
return customBtn
|
|
}
|
|
|
|
}
|