86 lines
3.3 KiB
Swift
86 lines
3.3 KiB
Swift
//
|
|
// UIButtom+.swift
|
|
// yinmeng-ios
|
|
//
|
|
// Created by yinmeng 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(type:UIButton.ButtonType = .custom, text:String? = nil,selectedText:String? = nil,disabledText:String? = nil,font:UIFont? = nil,color:UIColor? = nil,selectedColor:UIColor? = nil,disabledColor:UIColor? = nil,image:UIImage? = nil,selectedImage:UIImage? = nil,bgImage:UIImage? = nil,selectedBgImage:UIImage? = nil,disabledBgImage:UIImage? = nil,masksToBounds:Bool = false,cornerRadius:CGFloat = 0,borderWidth:CGFloat = 0,borderColor:UIColor? = nil) -> UIButton{
|
|
let customBtn = UIButton(type: type)
|
|
customBtn.layer.masksToBounds = masksToBounds
|
|
customBtn.layer.cornerRadius = cornerRadius
|
|
customBtn.layer.borderWidth = borderWidth
|
|
if let _borderColor = borderColor{
|
|
customBtn.layer.borderColor = _borderColor.cgColor
|
|
}
|
|
if let _text = text{
|
|
customBtn.setTitle(_text, for: .normal)
|
|
}
|
|
if let _selectedText = selectedText{
|
|
customBtn.setTitle(_selectedText, for: .selected)
|
|
}
|
|
if let _disabledText = disabledText{
|
|
customBtn.setTitle(_disabledText, for: .disabled)
|
|
}
|
|
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 _disabledColor = disabledColor{
|
|
customBtn.setTitleColor(_disabledColor, for: .disabled)
|
|
}
|
|
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)
|
|
}
|
|
if let _selectedBgImage = selectedBgImage{
|
|
customBtn.setBackgroundImage(_selectedBgImage, for: .selected)
|
|
}
|
|
if let _disabledBgImage = disabledBgImage{
|
|
customBtn.setBackgroundImage(_disabledBgImage, for: .disabled)
|
|
}
|
|
return customBtn
|
|
}
|
|
|
|
}
|