UIView+YYAdd.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // UIView+YYAdd.m
  3. // YYCategories <https://github.com/ibireme/YYCategories>
  4. //
  5. // Created by ibireme on 13/4/3.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "UIView+YYAdd.h"
  12. #import <QuartzCore/QuartzCore.h>
  13. @implementation UIView (YYAdd)
  14. - (void)setLayerShadow:(UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius {
  15. self.layer.shadowColor = color.CGColor;
  16. self.layer.shadowOffset = offset;
  17. self.layer.shadowRadius = radius;
  18. self.layer.shadowOpacity = 1;
  19. self.layer.shouldRasterize = YES;
  20. self.layer.rasterizationScale = [UIScreen mainScreen].scale;
  21. }
  22. - (void)removeAllSubviews {
  23. //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  24. while (self.subviews.count) {
  25. [self.subviews.lastObject removeFromSuperview];
  26. }
  27. }
  28. - (UIViewController *)viewController {
  29. for (UIView *view = self; view; view = view.superview) {
  30. UIResponder *nextResponder = [view nextResponder];
  31. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  32. return (UIViewController *)nextResponder;
  33. }
  34. }
  35. return nil;
  36. }
  37. - (CGFloat)left {
  38. return self.frame.origin.x;
  39. }
  40. - (void)setLeft:(CGFloat)x {
  41. CGRect frame = self.frame;
  42. frame.origin.x = x;
  43. self.frame = frame;
  44. }
  45. - (CGFloat)top {
  46. return self.frame.origin.y;
  47. }
  48. - (void)setTop:(CGFloat)y {
  49. CGRect frame = self.frame;
  50. frame.origin.y = y;
  51. self.frame = frame;
  52. }
  53. - (CGFloat)right {
  54. return self.frame.origin.x + self.frame.size.width;
  55. }
  56. - (void)setRight:(CGFloat)right {
  57. CGRect frame = self.frame;
  58. frame.origin.x = right - frame.size.width;
  59. self.frame = frame;
  60. }
  61. - (CGFloat)bottom {
  62. return self.frame.origin.y + self.frame.size.height;
  63. }
  64. - (void)setBottom:(CGFloat)bottom {
  65. CGRect frame = self.frame;
  66. frame.origin.y = bottom - frame.size.height;
  67. self.frame = frame;
  68. }
  69. - (CGFloat)width {
  70. return self.frame.size.width;
  71. }
  72. - (void)setWidth:(CGFloat)width {
  73. CGRect frame = self.frame;
  74. frame.size.width = width;
  75. self.frame = frame;
  76. }
  77. - (CGFloat)height {
  78. return self.frame.size.height;
  79. }
  80. - (void)setHeight:(CGFloat)height {
  81. CGRect frame = self.frame;
  82. frame.size.height = height;
  83. self.frame = frame;
  84. }
  85. - (CGFloat)centerX {
  86. return self.center.x;
  87. }
  88. - (void)setCenterX:(CGFloat)centerX {
  89. self.center = CGPointMake(centerX, self.center.y);
  90. }
  91. - (CGFloat)centerY {
  92. return self.center.y;
  93. }
  94. - (void)setCenterY:(CGFloat)centerY {
  95. self.center = CGPointMake(self.center.x, centerY);
  96. }
  97. - (CGPoint)origin {
  98. return self.frame.origin;
  99. }
  100. - (void)setOrigin:(CGPoint)origin {
  101. CGRect frame = self.frame;
  102. frame.origin = origin;
  103. self.frame = frame;
  104. }
  105. - (CGSize)size {
  106. return self.frame.size;
  107. }
  108. - (void)setSize:(CGSize)size {
  109. CGRect frame = self.frame;
  110. frame.size = size;
  111. self.frame = frame;
  112. }
  113. @end
  114. double YYDeviceSystemVersion() {
  115. static double version;
  116. static dispatch_once_t onceToken;
  117. dispatch_once(&onceToken, ^{
  118. version = [UIDevice currentDevice].systemVersion.doubleValue;
  119. });
  120. return version;
  121. }
  122. CGSize YYDeviceScreenSize() {
  123. static CGSize size;
  124. static dispatch_once_t onceToken;
  125. dispatch_once(&onceToken, ^{
  126. size = [UIScreen mainScreen].bounds.size;
  127. if (size.height <= size.width) {
  128. CGFloat tmp = size.height;
  129. size.height = size.width;
  130. size.width = tmp;
  131. }
  132. });
  133. return size;
  134. }
  135. @implementation UIColor (YYAdd)
  136. static inline NSUInteger hexStrToInt(NSString *str) {
  137. uint32_t result = 0;
  138. sscanf([str UTF8String], "%X", &result);
  139. return result;
  140. }
  141. static BOOL hexStrToRGBA(NSString *str,
  142. CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
  143. str = [str uppercaseString];
  144. if ([str hasPrefix:@"#"]) {
  145. str = [str substringFromIndex:1];
  146. } else if ([str hasPrefix:@"0X"]) {
  147. str = [str substringFromIndex:2];
  148. }
  149. NSUInteger length = [str length];
  150. // RGB RGBA RRGGBB RRGGBBAA
  151. if (length != 3 && length != 4 && length != 6 && length != 8) {
  152. return NO;
  153. }
  154. //RGB,RGBA,RRGGBB,RRGGBBAA
  155. if (length < 5) {
  156. *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
  157. *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
  158. *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
  159. if (length == 4) *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
  160. else *a = 1;
  161. } else {
  162. *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
  163. *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
  164. *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
  165. if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
  166. else *a = 1;
  167. }
  168. return YES;
  169. }
  170. + (instancetype)colorWithHexString:(NSString *)hexStr {
  171. CGFloat r, g, b, a;
  172. if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) {
  173. return [UIColor colorWithRed:r green:g blue:b alpha:a];
  174. }
  175. return nil;
  176. }
  177. - (UIColor *)colorByAddColor:(UIColor *)add blendMode:(CGBlendMode)blendMode {
  178. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  179. CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
  180. uint8_t pixel[4] = { 0 };
  181. CGContextRef context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, bitmapInfo);
  182. CGContextSetFillColorWithColor(context, self.CGColor);
  183. CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
  184. CGContextSetBlendMode(context, blendMode);
  185. CGContextSetFillColorWithColor(context, add.CGColor);
  186. CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
  187. CGContextRelease(context);
  188. CGColorSpaceRelease(colorSpace);
  189. return [UIColor colorWithRed:pixel[0] / 255.0f green:pixel[1] / 255.0f blue:pixel[2] / 255.0f alpha:pixel[3] / 255.0f];
  190. }
  191. @end