// // UIView+YYAdd.m // YYCategories // // Created by ibireme on 13/4/3. // Copyright (c) 2015 ibireme. // // This source code is licensed under the MIT-style license found in the // LICENSE file in the root directory of this source tree. // #import "UIView+YYAdd.h" #import @implementation UIView (YYAdd) - (void)setLayerShadow:(UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius { self.layer.shadowColor = color.CGColor; self.layer.shadowOffset = offset; self.layer.shadowRadius = radius; self.layer.shadowOpacity = 1; self.layer.shouldRasterize = YES; self.layer.rasterizationScale = [UIScreen mainScreen].scale; } - (void)removeAllSubviews { //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; while (self.subviews.count) { [self.subviews.lastObject removeFromSuperview]; } } - (UIViewController *)viewController { for (UIView *view = self; view; view = view.superview) { UIResponder *nextResponder = [view nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) { return (UIViewController *)nextResponder; } } return nil; } - (CGFloat)left { return self.frame.origin.x; } - (void)setLeft:(CGFloat)x { CGRect frame = self.frame; frame.origin.x = x; self.frame = frame; } - (CGFloat)top { return self.frame.origin.y; } - (void)setTop:(CGFloat)y { CGRect frame = self.frame; frame.origin.y = y; self.frame = frame; } - (CGFloat)right { return self.frame.origin.x + self.frame.size.width; } - (void)setRight:(CGFloat)right { CGRect frame = self.frame; frame.origin.x = right - frame.size.width; self.frame = frame; } - (CGFloat)bottom { return self.frame.origin.y + self.frame.size.height; } - (void)setBottom:(CGFloat)bottom { CGRect frame = self.frame; frame.origin.y = bottom - frame.size.height; self.frame = frame; } - (CGFloat)width { return self.frame.size.width; } - (void)setWidth:(CGFloat)width { CGRect frame = self.frame; frame.size.width = width; self.frame = frame; } - (CGFloat)height { return self.frame.size.height; } - (void)setHeight:(CGFloat)height { CGRect frame = self.frame; frame.size.height = height; self.frame = frame; } - (CGFloat)centerX { return self.center.x; } - (void)setCenterX:(CGFloat)centerX { self.center = CGPointMake(centerX, self.center.y); } - (CGFloat)centerY { return self.center.y; } - (void)setCenterY:(CGFloat)centerY { self.center = CGPointMake(self.center.x, centerY); } - (CGPoint)origin { return self.frame.origin; } - (void)setOrigin:(CGPoint)origin { CGRect frame = self.frame; frame.origin = origin; self.frame = frame; } - (CGSize)size { return self.frame.size; } - (void)setSize:(CGSize)size { CGRect frame = self.frame; frame.size = size; self.frame = frame; } @end float YYDeviceSystemVersion() { static float version; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ version = [UIDevice currentDevice].systemVersion.floatValue; }); return version; } CGSize YYDeviceScreenSize() { static CGSize size; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ size = [UIScreen mainScreen].bounds.size; if (size.height <= size.width) { CGFloat tmp = size.height; size.height = size.width; size.width = tmp; } }); return size; } @implementation UIColor (YYAdd) static inline NSUInteger hexStrToInt(NSString *str) { uint32_t result = 0; sscanf([str UTF8String], "%X", &result); return result; } static BOOL hexStrToRGBA(NSString *str, CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) { str = [str uppercaseString]; if ([str hasPrefix:@"#"]) { str = [str substringFromIndex:1]; } else if ([str hasPrefix:@"0X"]) { str = [str substringFromIndex:2]; } NSUInteger length = [str length]; // RGB RGBA RRGGBB RRGGBBAA if (length != 3 && length != 4 && length != 6 && length != 8) { return NO; } //RGB,RGBA,RRGGBB,RRGGBBAA if (length < 5) { *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f; *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f; *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f; if (length == 4) *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f; else *a = 1; } else { *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f; *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f; *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f; if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f; else *a = 1; } return YES; } + (instancetype)colorWithHexString:(NSString *)hexStr { CGFloat r, g, b, a; if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) { return [UIColor colorWithRed:r green:g blue:b alpha:a]; } return nil; } - (UIColor *)colorByAddColor:(UIColor *)add blendMode:(CGBlendMode)blendMode { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big; uint8_t pixel[4] = { 0 }; CGContextRef context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, bitmapInfo); CGContextSetFillColorWithColor(context, self.CGColor); CGContextFillRect(context, CGRectMake(0, 0, 1, 1)); CGContextSetBlendMode(context, blendMode); CGContextSetFillColorWithColor(context, add.CGColor); CGContextFillRect(context, CGRectMake(0, 0, 1, 1)); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return [UIColor colorWithRed:pixel[0] / 255.0f green:pixel[1] / 255.0f blue:pixel[2] / 255.0f alpha:pixel[3] / 255.0f]; } @end