Files
yingmeng-ios-switf/yinmeng-ios/Extension/UIButton/UIButton+.swift

86 lines
3.3 KiB
Swift
Raw Normal View History

2024-03-07 16:56:48 +08:00
//
// UIButtom+.swift
// yinmeng-ios
//
// Created by yinmeng on 2024/3/6.
2024-03-07 16:56:48 +08:00
//
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)
}
2024-03-23 16:28:10 +08:00
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{
2024-03-22 11:54:23 +08:00
let customBtn = UIButton(type: type)
2024-03-23 16:28:10 +08:00
customBtn.layer.masksToBounds = masksToBounds
customBtn.layer.cornerRadius = cornerRadius
customBtn.layer.borderWidth = borderWidth
if let _borderColor = borderColor{
customBtn.layer.borderColor = _borderColor.cgColor
}
2024-03-07 16:56:48 +08:00
if let _text = text{
2024-03-22 11:54:23 +08:00
customBtn.setTitle(_text, for: .normal)
2024-03-07 16:56:48 +08:00
}
if let _selectedText = selectedText{
2024-03-22 11:54:23 +08:00
customBtn.setTitle(_selectedText, for: .selected)
2024-03-07 16:56:48 +08:00
}
2024-03-23 16:28:10 +08:00
if let _disabledText = disabledText{
customBtn.setTitle(_disabledText, for: .disabled)
}
2024-03-07 16:56:48 +08:00
if let _font = font{
2024-03-22 11:54:23 +08:00
customBtn.titleLabel?.font = _font
2024-03-07 16:56:48 +08:00
}
if let _color = color{
2024-03-22 11:54:23 +08:00
customBtn.setTitleColor(_color, for: .normal)
2024-03-07 16:56:48 +08:00
}
if let _selectedColor = selectedColor{
2024-03-22 11:54:23 +08:00
customBtn.setTitleColor(_selectedColor, for: .selected)
2024-03-07 16:56:48 +08:00
}
2024-03-23 16:28:10 +08:00
if let _disabledColor = disabledColor{
customBtn.setTitleColor(_disabledColor, for: .disabled)
}
2024-03-07 16:56:48 +08:00
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)
}
2024-03-22 11:54:23 +08:00
if let _selectedBgImage = selectedBgImage{
customBtn.setBackgroundImage(_selectedBgImage, for: .selected)
}
2024-03-23 16:28:10 +08:00
if let _disabledBgImage = disabledBgImage{
customBtn.setBackgroundImage(_disabledBgImage, for: .disabled)
}
2024-03-07 16:56:48 +08:00
return customBtn
}
}