Skip to content

Instantly share code, notes, and snippets.

@colejd
Last active August 10, 2018 00:28
Show Gist options
  • Save colejd/a2a07d9c34fffd511e0a80a88407cbea to your computer and use it in GitHub Desktop.
Save colejd/a2a07d9c34fffd511e0a80a88407cbea to your computer and use it in GitHub Desktop.
Swizzles in a randomized color block in place of the cgColor getter on UIColor, randomizing most colors throughout the app.
public extension UIColor {
private static let rzl_swizzleImplementation: Void = {
let instance: UIColor = UIColor.red // This is a `UICachedDeviceRGBColor` instance. For some reason you have to use red.
let _class: AnyClass! = object_getClass(instance)
let originalMethod = class_getInstanceMethod(_class, #selector(getter: cgColor))
let swizzledMethod = class_getInstanceMethod(_class, #selector(rzl_randomCGColor))
if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}()
public static func rzl_swizzle() {
_ = self.rzl_swizzleImplementation
}
dynamic func rzl_randomCGColor() -> CGColor {
return CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [
CGFloat(Float(arc4random()) / Float(UINT32_MAX)),
CGFloat(Float(arc4random()) / Float(UINT32_MAX)),
CGFloat(Float(arc4random()) / Float(UINT32_MAX)),
CGFloat(Float(arc4random()) / Float(UINT32_MAX)),
])!
}
}
@colejd
Copy link
Author

colejd commented Aug 10, 2018

I made a version that will replicate the functionality of the better, App-Store-unsafe way in a way that will probably not get you rejected: https://gist.github.com/colejd/e41fc68f8335670999a7859df0d6fa93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment