FLEXUtility.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // FLEXUtility.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/18/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXUtility.h"
  9. #import "FLEXResources.h"
  10. @implementation FLEXUtility
  11. + (UIColor *)consistentRandomColorForObject:(id)object
  12. {
  13. CGFloat hue = (((NSUInteger)object >> 4) % 256) / 255.0;
  14. return [UIColor colorWithHue:hue saturation:1.0 brightness:1.0 alpha:1.0];
  15. }
  16. + (NSString *)descriptionForView:(UIView *)view includingFrame:(BOOL)includeFrame
  17. {
  18. NSString *description = [[view class] description];
  19. NSString *viewControllerDescription = [[[self viewControllerForView:view] class] description];
  20. if ([viewControllerDescription length] > 0) {
  21. description = [description stringByAppendingFormat:@" (%@)", viewControllerDescription];
  22. }
  23. if (includeFrame) {
  24. description = [description stringByAppendingFormat:@" %@", [self stringForCGRect:view.frame]];
  25. }
  26. if ([view.accessibilityLabel length] > 0) {
  27. description = [description stringByAppendingFormat:@" · %@", view.accessibilityLabel];
  28. }
  29. return description;
  30. }
  31. + (NSString *)stringForCGRect:(CGRect)rect
  32. {
  33. return [NSString stringWithFormat:@"{(%g, %g), (%g, %g)}", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height];
  34. }
  35. + (UIViewController *)viewControllerForView:(UIView *)view
  36. {
  37. UIViewController *viewController = nil;
  38. SEL viewDelSel = NSSelectorFromString([NSString stringWithFormat:@"%@ewDelegate", @"_vi"]);
  39. if ([view respondsToSelector:viewDelSel]) {
  40. #pragma clang diagnostic push
  41. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  42. viewController = [view performSelector:viewDelSel];
  43. #pragma clang diagnostic pop
  44. }
  45. return viewController;
  46. }
  47. + (NSString *)detailDescriptionForView:(UIView *)view
  48. {
  49. return [NSString stringWithFormat:@"frame %@", [self stringForCGRect:view.frame]];
  50. }
  51. + (UIImage *)circularImageWithColor:(UIColor *)color radius:(CGFloat)radius
  52. {
  53. CGFloat diameter = radius * 2.0;
  54. UIGraphicsBeginImageContextWithOptions(CGSizeMake(diameter, diameter), NO, 0.0);
  55. CGContextRef imageContext = UIGraphicsGetCurrentContext();
  56. CGContextSetFillColorWithColor(imageContext, [color CGColor]);
  57. CGContextFillEllipseInRect(imageContext, CGRectMake(0, 0, diameter, diameter));
  58. UIImage *circularImage = UIGraphicsGetImageFromCurrentImageContext();
  59. UIGraphicsEndImageContext();
  60. return circularImage;
  61. }
  62. + (UIColor *)scrollViewGrayColor
  63. {
  64. return [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:244.0/255.0 alpha:1.0];
  65. }
  66. + (UIColor *)hierarchyIndentPatternColor
  67. {
  68. static UIColor *patternColor = nil;
  69. static dispatch_once_t onceToken;
  70. dispatch_once(&onceToken, ^{
  71. UIImage *indentationPatternImage = [FLEXResources hierarchyIndentPattern];
  72. patternColor = [UIColor colorWithPatternImage:indentationPatternImage];
  73. });
  74. return patternColor;
  75. }
  76. + (NSString *)applicationImageName
  77. {
  78. return [[NSBundle mainBundle] executablePath];
  79. }
  80. + (NSString *)applicationName
  81. {
  82. return [[[FLEXUtility applicationImageName] componentsSeparatedByString:@"/"] lastObject];
  83. }
  84. + (NSString *)safeDescriptionForObject:(id)object
  85. {
  86. // Don't assume that we have an NSObject subclass.
  87. // Check to make sure the object responds to the description methods.
  88. NSString *description = nil;
  89. if ([object respondsToSelector:@selector(debugDescription)]) {
  90. description = [object debugDescription];
  91. } else if ([object respondsToSelector:@selector(description)]) {
  92. description = [object description];
  93. }
  94. return description;
  95. }
  96. + (UIFont *)defaultFontOfSize:(CGFloat)size
  97. {
  98. return [UIFont fontWithName:@"HelveticaNeue" size:size];
  99. }
  100. + (UIFont *)defaultTableViewCellLabelFont
  101. {
  102. return [self defaultFontOfSize:12.0];
  103. }
  104. + (NSString *)stringByEscapingHTMLEntitiesInString:(NSString *)originalString
  105. {
  106. static NSDictionary *escapingDictionary = nil;
  107. static NSRegularExpression *regex = nil;
  108. static dispatch_once_t onceToken;
  109. dispatch_once(&onceToken, ^{
  110. escapingDictionary = @{ @" " : @" ",
  111. @">" : @">",
  112. @"<" : @"&lt;",
  113. @"&" : @"&amp;",
  114. @"'" : @"&apos;",
  115. @"\"" : @"&quot;",
  116. @"«" : @"&laquo;",
  117. @"»" : @"&raquo;"
  118. };
  119. regex = [NSRegularExpression regularExpressionWithPattern:@"(&|>|<|'|\"|«|»)" options:0 error:NULL];
  120. });
  121. NSMutableString *mutableString = [originalString mutableCopy];
  122. NSArray *matches = [regex matchesInString:mutableString options:0 range:NSMakeRange(0, [mutableString length])];
  123. for (NSTextCheckingResult *result in [matches reverseObjectEnumerator]) {
  124. NSString *foundString = [mutableString substringWithRange:result.range];
  125. NSString *replacementString = escapingDictionary[foundString];
  126. if (replacementString) {
  127. [mutableString replaceCharactersInRange:result.range withString:replacementString];
  128. }
  129. }
  130. return [mutableString copy];
  131. }
  132. + (UIInterfaceOrientationMask)infoPlistSupportedInterfaceOrientationsMask
  133. {
  134. NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
  135. UIInterfaceOrientationMask supportedOrientationsMask = 0;
  136. if ([supportedOrientations containsObject:@"UIInterfaceOrientationPortrait"]) {
  137. supportedOrientationsMask |= UIInterfaceOrientationMaskPortrait;
  138. }
  139. if ([supportedOrientations containsObject:@"UIInterfaceOrientationMaskLandscapeRight"]) {
  140. supportedOrientationsMask |= UIInterfaceOrientationMaskLandscapeRight;
  141. }
  142. if ([supportedOrientations containsObject:@"UIInterfaceOrientationMaskPortraitUpsideDown"]) {
  143. supportedOrientationsMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  144. }
  145. if ([supportedOrientations containsObject:@"UIInterfaceOrientationLandscapeLeft"]) {
  146. supportedOrientationsMask |= UIInterfaceOrientationMaskLandscapeLeft;
  147. }
  148. return supportedOrientationsMask;
  149. }
  150. + (NSString *)searchBarPlaceholderText
  151. {
  152. return @"Filter";
  153. }
  154. + (BOOL)isImagePathExtension:(NSString *)extension
  155. {
  156. // https://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006890-CH3-SW3
  157. return [@[@"jpg", @"jpeg", @"png", @"gif", @"tiff", @"tif"] containsObject:extension];
  158. }
  159. @end