FLEXUtility.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // FLEXUtility.h
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/18/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import <Availability.h>
  9. #import <AvailabilityInternal.h>
  10. #import <Foundation/Foundation.h>
  11. #import <UIKit/UIKit.h>
  12. #import <objc/runtime.h>
  13. #import "FLEXTypeEncodingParser.h"
  14. #import "FLEXAlert.h"
  15. #import "NSArray+Functional.h"
  16. #import "UIFont+FLEX.h"
  17. #import "NSMapTable+FLEX_Subscripting.h"
  18. /// Rounds down to the nearest "point" coordinate
  19. NS_INLINE CGFloat FLEXFloor(CGFloat x) {
  20. return floor(UIScreen.mainScreen.scale * (x)) / UIScreen.mainScreen.scale;
  21. }
  22. /// Creates a CGRect with all members rounded down to the nearest "point" coordinate
  23. NS_INLINE CGRect FLEXRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height) {
  24. return CGRectMake(FLEXFloor(x), FLEXFloor(y), FLEXFloor(width), FLEXFloor(height));
  25. }
  26. /// Adjusts the origin of an existing rect
  27. NS_INLINE CGRect FLEXRectSetOrigin(CGRect r, CGPoint origin) {
  28. r.origin = origin; return r;
  29. }
  30. /// Adjusts the size of an existing rect
  31. NS_INLINE CGRect FLEXRectSetSize(CGRect r, CGSize size) {
  32. r.size = size; return r;
  33. }
  34. /// Adjusts the origin.x of an existing rect
  35. NS_INLINE CGRect FLEXRectSetX(CGRect r, CGFloat x) {
  36. r.origin.x = x; return r;
  37. }
  38. /// Adjusts the origin.y of an existing rect
  39. NS_INLINE CGRect FLEXRectSetY(CGRect r, CGFloat y) {
  40. r.origin.y = y ; return r;
  41. }
  42. /// Adjusts the size.width of an existing rect
  43. NS_INLINE CGRect FLEXRectSetWidth(CGRect r, CGFloat width) {
  44. r.size.width = width; return r;
  45. }
  46. /// Adjusts the size.height of an existing rect
  47. NS_INLINE CGRect FLEXRectSetHeight(CGRect r, CGFloat height) {
  48. r.size.height = height; return r;
  49. }
  50. #ifdef __IPHONE_13_0
  51. #define FLEX_AT_LEAST_IOS13_SDK (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
  52. #else
  53. #define FLEX_AT_LEAST_IOS13_SDK NO
  54. #endif
  55. #define FLEXPluralString(count, plural, singular) [NSString \
  56. stringWithFormat:@"%@ %@", @(count), (count == 1 ? singular : plural) \
  57. ]
  58. #define FLEXPluralFormatString(count, pluralFormat, singularFormat) [NSString \
  59. stringWithFormat:(count == 1 ? singularFormat : pluralFormat), @(count) \
  60. ]
  61. #if !FLEX_AT_LEAST_IOS13_SDK
  62. @class UIWindowScene;
  63. #endif
  64. @interface FLEXUtility : NSObject
  65. /// The key window of the app, if it is not a \c FLEXWindow.
  66. /// If it is, then \c FLEXWindow.previousKeyWindow is returned.
  67. @property (nonatomic, readonly, class) UIWindow *appKeyWindow;
  68. /// The first active \c UIWindowScene of the app.
  69. @property (nonatomic, readonly, class) UIWindowScene *activeScene API_AVAILABLE(ios(13.0));
  70. /// @return top-most view controller of the given window
  71. + (UIViewController *)topViewControllerInWindow:(UIWindow *)window;
  72. + (UIColor *)consistentRandomColorForObject:(id)object;
  73. + (NSString *)descriptionForView:(UIView *)view includingFrame:(BOOL)includeFrame;
  74. + (NSString *)stringForCGRect:(CGRect)rect;
  75. + (UIViewController *)viewControllerForView:(UIView *)view;
  76. + (UIViewController *)viewControllerForAncestralView:(UIView *)view;
  77. + (UIImage *)previewImageForView:(UIView *)view;
  78. + (UIImage *)previewImageForLayer:(CALayer *)layer;
  79. + (NSString *)detailDescriptionForView:(UIView *)view;
  80. + (UIImage *)circularImageWithColor:(UIColor *)color radius:(CGFloat)radius;
  81. + (UIColor *)hierarchyIndentPatternColor;
  82. + (NSString *)pointerToString:(void *)ptr;
  83. + (NSString *)addressOfObject:(id)object;
  84. + (NSString *)stringByEscapingHTMLEntitiesInString:(NSString *)originalString;
  85. + (UIInterfaceOrientationMask)infoPlistSupportedInterfaceOrientationsMask;
  86. + (UIImage *)thumbnailedImageWithMaxPixelDimension:(NSInteger)dimension fromImageData:(NSData *)data;
  87. + (NSString *)stringFromRequestDuration:(NSTimeInterval)duration;
  88. + (NSString *)statusCodeStringFromURLResponse:(NSURLResponse *)response;
  89. + (BOOL)isErrorStatusCodeFromURLResponse:(NSURLResponse *)response;
  90. + (NSArray<NSURLQueryItem *> *)itemsFromQueryString:(NSString *)query;
  91. + (NSString *)prettyJSONStringFromData:(NSData *)data;
  92. + (BOOL)isValidJSONData:(NSData *)data;
  93. + (NSData *)inflatedDataFromCompressedData:(NSData *)compressedData;
  94. + (NSArray<UIWindow *> *)allWindows;
  95. // Swizzling utilities
  96. + (SEL)swizzledSelectorForSelector:(SEL)selector;
  97. + (BOOL)instanceRespondsButDoesNotImplementSelector:(SEL)selector class:(Class)cls;
  98. + (void)replaceImplementationOfKnownSelector:(SEL)originalSelector onClass:(Class)class withBlock:(id)block swizzledSelector:(SEL)swizzledSelector;
  99. + (void)replaceImplementationOfSelector:(SEL)selector withSelector:(SEL)swizzledSelector forClass:(Class)cls withMethodDescription:(struct objc_method_description)methodDescription implementationBlock:(id)implementationBlock undefinedBlock:(id)undefinedBlock;
  100. @end