Created
May 31, 2024 09:21
-
-
Save simonbs/11bf9b0cc02eea08a3b1f50d813c5db3 to your computer and use it in GitHub Desktop.
UISearchAtomView+Swizzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum UISearchAtomViewSwizzler { | |
private static var searchAtomViewClass: AnyClass? { | |
NSClassFromString("_UISearchAtomView") | |
} | |
private static var searchAtomViewVisualStyleClass: AnyClass? { | |
NSClassFromString("_UISearchAtomViewVisualStyle_default") | |
} | |
static func swizzle() { | |
swizzleLayoutSubviews() | |
swizzleImageTextSpacing() | |
swizzleContentHorizontalPadding() | |
swizzleDefaultTextAndImageColor() | |
swizzleDefaultAtomBackgroundColor() | |
} | |
private static func swizzleLayoutSubviews() { | |
let originalSelector = #selector(UIView.layoutSubviews) | |
let swizzledSelector = #selector(UIView.shp_layoutSubviews) | |
if let searchAtomViewClass { | |
NSObject.swizzleMethod(originalSelector, on: searchAtomViewClass, with: swizzledSelector, on: UIView.self) | |
} | |
} | |
private static func swizzleImageTextSpacing() { | |
let originalSelector1 = NSSelectorFromString("imageTextSpacing") | |
let swizzledSelector1 = #selector(getter: UISearchAtomVisualStyle.shp_imageTextSpacing) | |
if let searchAtomViewVisualStyleClass { | |
NSObject.swizzleMethod( | |
originalSelector1, | |
on: searchAtomViewVisualStyleClass, | |
with: swizzledSelector1, | |
on: UISearchAtomVisualStyle.self | |
) | |
} | |
} | |
private static func swizzleContentHorizontalPadding() { | |
let originalSelector1 = NSSelectorFromString("contentHorizontalPadding") | |
let swizzledSelector1 = #selector(getter: UISearchAtomVisualStyle.shp_contentHorizontalPadding) | |
if let searchAtomViewVisualStyleClass { | |
NSObject.swizzleMethod( | |
originalSelector1, | |
on: searchAtomViewVisualStyleClass, | |
with: swizzledSelector1, | |
on: UISearchAtomVisualStyle.self | |
) | |
} | |
} | |
private static func swizzleDefaultTextAndImageColor() { | |
let originalSelector1 = NSSelectorFromString("defaultTextAndImageColor") | |
let swizzledSelector1 = #selector(getter: UISearchAtomVisualStyle.shp_defaultTextAndImageColor) | |
if let searchAtomViewVisualStyleClass { | |
NSObject.swizzleMethod( | |
originalSelector1, | |
on: searchAtomViewVisualStyleClass, | |
with: swizzledSelector1, | |
on: UISearchAtomVisualStyle.self | |
) | |
} | |
} | |
private static func swizzleDefaultAtomBackgroundColor() { | |
let originalSelector1 = NSSelectorFromString("defaultAtomBackgroundColor") | |
let swizzledSelector1 = #selector(getter: UISearchAtomVisualStyle.shp_defaultAtomBackgroundColor) | |
if let searchAtomViewVisualStyleClass { | |
NSObject.swizzleMethod( | |
originalSelector1, | |
on: searchAtomViewVisualStyleClass, | |
with: swizzledSelector1, | |
on: UISearchAtomVisualStyle.self | |
) | |
} | |
} | |
} | |
private final class UISearchAtomVisualStyle { | |
@objc var shp_imageTextSpacing: Double { | |
1 | |
} | |
@objc var shp_contentHorizontalPadding: Double { | |
2 | |
} | |
@objc var shp_defaultTextAndImageColor: UIColor { | |
.mooodLabelPrimary | |
} | |
@objc var shp_defaultAtomBackgroundColor: UIColor { | |
.mooodLabelSecondary.withAlphaComponent(0.4) | |
} | |
} | |
private extension UIView { | |
private var shp_searchAtomTitleLabel: UILabel? { | |
subviews.compactMap { $0 as? UILabel }.first | |
} | |
@objc func shp_layoutSubviews() { | |
self.shp_layoutSubviews() | |
shp_searchAtomTitleLabel?.font = .framnaSans(size: 15, ofWeight: .medium, isScaled: false) | |
} | |
} | |
extension NSObject { | |
static func swizzleMethod(_ originalSelector: Selector, on originalClass: AnyClass, with swizzledSelector: Selector, on swizzledClass: AnyClass) { | |
guard let originalMethod = class_getInstanceMethod(originalClass, originalSelector) else { | |
return | |
} | |
guard let swizzledMethod = class_getInstanceMethod(swizzledClass, swizzledSelector) else { | |
return | |
} | |
let didAddMethod = class_addMethod( | |
originalClass, | |
originalSelector, | |
method_getImplementation(swizzledMethod), | |
method_getTypeEncoding(swizzledMethod) | |
) | |
if didAddMethod { | |
class_replaceMethod( | |
swizzledClass, | |
swizzledSelector, | |
method_getImplementation(originalMethod), | |
method_getTypeEncoding(originalMethod) | |
) | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment