123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- //
- // UIView+YYAdd.m
- // YYCategories <https://github.com/ibireme/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 <QuartzCore/QuartzCore.h>
- @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
|