FLEXUtility.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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:@" frame:%@", NSStringFromCGRect(view.frame)];
  25. }
  26. if ([view.accessibilityLabel length] > 0) {
  27. description = [description stringByAppendingFormat:@" · %@", view.accessibilityLabel];
  28. }
  29. return description;
  30. }
  31. + (UIViewController *)viewControllerForView:(UIView *)view
  32. {
  33. UIViewController *viewController = nil;
  34. SEL viewDelSel = NSSelectorFromString([NSString stringWithFormat:@"%@ewDelegate", @"_vi"]);
  35. if ([view respondsToSelector:viewDelSel]) {
  36. #pragma clang diagnostic push
  37. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  38. viewController = [view performSelector:viewDelSel];
  39. #pragma clang diagnostic pop
  40. }
  41. return viewController;
  42. }
  43. + (NSString *)detailDescriptionForView:(UIView *)view
  44. {
  45. return [NSString stringWithFormat:@"frame = %@", NSStringFromCGRect(view.frame)];
  46. }
  47. + (UIImage *)circularImageWithColor:(UIColor *)color radius:(CGFloat)radius
  48. {
  49. CGFloat diameter = radius * 2.0;
  50. UIGraphicsBeginImageContextWithOptions(CGSizeMake(diameter, diameter), NO, 0.0);
  51. CGContextRef imageContext = UIGraphicsGetCurrentContext();
  52. CGContextSetFillColorWithColor(imageContext, [color CGColor]);
  53. CGContextFillEllipseInRect(imageContext, CGRectMake(0, 0, diameter, diameter));
  54. UIImage *circularImage = UIGraphicsGetImageFromCurrentImageContext();
  55. UIGraphicsEndImageContext();
  56. return circularImage;
  57. }
  58. + (UIColor *)scrollViewGrayColor
  59. {
  60. return [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:244.0/255.0 alpha:1.0];
  61. }
  62. + (UIColor *)hierarchyIndentPatternColor
  63. {
  64. static UIColor *patternColor = nil;
  65. static dispatch_once_t onceToken;
  66. dispatch_once(&onceToken, ^{
  67. UIImage *indentationPatternImage = [FLEXResources hierarchyIndentPattern];
  68. patternColor = [UIColor colorWithPatternImage:indentationPatternImage];
  69. });
  70. return patternColor;
  71. }
  72. + (NSString *)applicationImageName
  73. {
  74. return [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
  75. }
  76. + (NSString *)applicationName
  77. {
  78. return [[[FLEXUtility applicationImageName] componentsSeparatedByString:@"/"] lastObject];
  79. }
  80. + (NSString *)safeDescriptionForObject:(id)object
  81. {
  82. // Don't assume that we have an NSObject subclass.
  83. // Check to make sure the object responds to the description methods.
  84. NSString *description = nil;
  85. if ([object respondsToSelector:@selector(debugDescription)]) {
  86. description = [object debugDescription];
  87. } else if ([object respondsToSelector:@selector(description)]) {
  88. description = [object description];
  89. }
  90. return description;
  91. }
  92. + (UIFont *)defaultFontOfSize:(CGFloat)size
  93. {
  94. return [UIFont fontWithName:@"HelveticaNeue" size:size];
  95. }
  96. + (UIFont *)defaultTableViewCellLabelFont
  97. {
  98. return [self defaultFontOfSize:12.0];
  99. }
  100. + (NSString *)stringByEscapingHTMLEntitiesInString:(NSString *)originalString
  101. {
  102. static NSDictionary *escapingDictionary = nil;
  103. static NSRegularExpression *regex = nil;
  104. static dispatch_once_t onceToken;
  105. dispatch_once(&onceToken, ^{
  106. escapingDictionary = @{ @" " : @" ",
  107. @">" : @">",
  108. @"<" : @"&lt;",
  109. @"&" : @"&amp;",
  110. @"'" : @"&apos;",
  111. @"\"" : @"&quot;",
  112. @"«" : @"&laquo;",
  113. @"»" : @"&raquo;"
  114. };
  115. regex = [NSRegularExpression regularExpressionWithPattern:@"(&|>|<|'|\"|«|»)" options:0 error:nil];
  116. });
  117. NSMutableString *mutableString = [originalString mutableCopy];
  118. NSArray *matches = [regex matchesInString:mutableString options:0 range:NSMakeRange(0, [mutableString length])];
  119. for (NSTextCheckingResult *result in [matches reverseObjectEnumerator]) {
  120. NSString *foundString = [mutableString substringWithRange:result.range];
  121. NSString *replacementString = escapingDictionary[foundString];
  122. if (replacementString) {
  123. [mutableString replaceCharactersInRange:result.range withString:replacementString];
  124. }
  125. }
  126. return [mutableString copy];
  127. }
  128. + (NSUInteger)infoPlistSupportedInterfaceOrientationsMask
  129. {
  130. NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
  131. NSUInteger supportedOrientationsMask = 0;
  132. if ([supportedOrientations containsObject:@"UIInterfaceOrientationPortrait"]) {
  133. supportedOrientationsMask |= UIInterfaceOrientationPortrait;
  134. }
  135. if ([supportedOrientations containsObject:@"UIInterfaceOrientationMaskLandscapeRight"]) {
  136. supportedOrientationsMask |= UIInterfaceOrientationMaskLandscapeRight;
  137. }
  138. if ([supportedOrientations containsObject:@"UIInterfaceOrientationMaskPortraitUpsideDown"]) {
  139. supportedOrientationsMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  140. }
  141. if ([supportedOrientations containsObject:@"UIInterfaceOrientationLandscapeLeft"]) {
  142. supportedOrientationsMask |= UIInterfaceOrientationLandscapeLeft;
  143. }
  144. return supportedOrientationsMask;
  145. }
  146. + (NSString *)searchBarPlaceholderText
  147. {
  148. return @"Filter";
  149. }
  150. @end