jsdw_ios/QuickLocation/Core/Extension/UIButton+Extension.swift

89 lines
4.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// UIButton+Extension.swift
// dinoGo
//
// Created by osell on 2022/12/7.
// Copyright © 2022 dino. All rights reserved.
//
import Foundation
extension UIButton {
/// 设置图片的居中边位置,需要在setImage和setTitle之后调用才生效,且button大小大于图片+文字+间距
/// - Parameters:
/// - edge: 仅有image时相对于button,都有时上左下相对于button,右相对于title, 仅有title时相对于button,都有时上右下相对于button,左相对于image
/// - spacing: 距离
public func setImageEdge(_ edge: UIRectEdge, spacing: CGFloat) {
let imageSize = imageView?.image?.size ?? .zero
let labelSize = titleLabel?.intrinsicContentSize ?? .zero
switch edge {
case .left:
imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing / 2.0, bottom: 0, right: spacing / 2.0)
titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2.0, bottom: 0, right: -spacing / 2.0)
case .right:
imageEdgeInsets = UIEdgeInsets(top: 0, left: labelSize.width + spacing / 2.0, bottom: 0, right: -labelSize.width - spacing / 2.0)
titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageSize.width - spacing / 2.0, bottom: 0, right: imageSize.width + spacing / 2.0)
case .top:
imageEdgeInsets = UIEdgeInsets(top: -labelSize.height - spacing / 2.0, left: 0, bottom: spacing / 2.0, right: -labelSize.width)
titleEdgeInsets = UIEdgeInsets(top: spacing / 2.0, left: -imageSize.width, bottom: -imageSize.height - spacing / 2.0, right: 0)
case .bottom:
imageEdgeInsets = UIEdgeInsets(top: spacing / 2.0, left: 0, bottom: -labelSize.height - spacing / 2.0, right: -labelSize.width)
titleEdgeInsets = UIEdgeInsets(top: -imageSize.height - spacing / 2.0, left: -imageSize.width, bottom: spacing / 2.0, right: 0)
default:
break
}
}
/// 设置按钮背景颜色
/// - Parameters:
/// - backgroundColor: 颜色
/// - state: 状态
public func setBackgroundColor(_ backgroundColor: UIColor, for state: UIControl.State) {
var image: UIImage?
let rect = CGRect(origin: .zero, size: CGSize(width: 1, height: 1))
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(backgroundColor.cgColor)
context.fill(rect)
image = UIGraphicsGetImageFromCurrentImageContext()
}
UIGraphicsEndImageContext()
setBackgroundImage(image, for: state)
}
}
// MARK: - 扩大按钮点击区域
extension UIButton {
/// 设置此属性即可扩大响应范围, 分别对应上左下右
/// 优势:与Auto-Layout无缝配合
/// 劣势:View Debugger 查看不到增加的响应区域有多大,
private static var ExtendEdgeInsetsKey = "ExtendEdgeInsetsKey"
var extendEdgeInsets: UIEdgeInsets {
get {
return objc_getAssociatedObject(self, &UIButton.ExtendEdgeInsetsKey) as? UIEdgeInsets ?? UIEdgeInsets.zero
}
set {
objc_setAssociatedObject(self, &UIButton.ExtendEdgeInsetsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if UIEdgeInsetsEqualToEdgeInsets(extendEdgeInsets, .zero) || !self.isEnabled || self.isHidden || self.alpha < 0.01 {
return super.point(inside: point, with: event)
}
let newRect = extendRect(bounds, extendEdgeInsets)
return newRect.contains(point)
}
private func extendRect(_ rect: CGRect, _ edgeInsets: UIEdgeInsets) -> CGRect {
let x = rect.minX - edgeInsets.left
let y = rect.minY - edgeInsets.top
let w = rect.width + edgeInsets.left + edgeInsets.right
let h = rect.height + edgeInsets.top + edgeInsets.bottom
return CGRect(x: x, y: y, width: w, height: h)
}
}