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 {
/// setImagesetTitlebutton++
/// - Parameters:
/// - edge: imagebuttonbuttontitle, titlebuttonbuttonimage
/// - 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)
}
}