FLEXUtility.m 5.9 KB

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