//
//  UIView+HWCategory.m
//  YidaHealth
//
//  Created by 余衡武 on 2018/9/20.
//  Copyright © 2018年 余衡武. All rights reserved.
//

#import "UIView+HWCategory.h"

@implementation UIView (HWCategory)

- (void)setHw_x:(CGFloat)hw_x{
    CGRect frame = self.frame;
    frame.origin.x = hw_x;
    self.frame = frame;
}

- (CGFloat)hw_x{
    return self.frame.origin.x;
}

- (void)setHw_y:(CGFloat)hw_y{
    CGRect frame = self.frame;
    frame.origin.y = hw_y;
    self.frame = frame;
}

- (CGFloat)hw_y{
    return self.frame.origin.y;
}

- (void)setHw_w:(CGFloat)hw_w{
    CGRect frame = self.frame;
    frame.size.width = hw_w;
    self.frame = frame;
}

- (CGFloat)hw_w{
    return self.frame.size.width;
}

- (void)setHw_h:(CGFloat)hw_h{
    CGRect frame = self.frame;
    frame.size.height = hw_h;
    self.frame = frame;
}

- (CGFloat)hw_h{
    return self.frame.size.height;
}

- (void)setHw_center_x:(CGFloat)hw_center_x{
    CGPoint point = self.center;
    point.x = hw_center_x;
    self.center = point;
}

- (CGFloat)hw_center_x{
    return self.center.x;
}

- (void)setHw_center_y:(CGFloat)hw_center_y{
    CGPoint point = self.center;
    point.y = hw_center_y;
    self.center = point;
}

- (CGFloat)hw_center_y{
    return self.center.y;
}

- (void)setHw_origin:(CGPoint)hw_origin{
    CGRect frame = self.frame;
    frame.origin.x = hw_origin.x;
    frame.origin.y = hw_origin.y;
    self.frame = frame;
}

- (CGPoint)hw_origin{
    return self.frame.origin;
}

- (void)setHw_size:(CGSize)hw_size{
    CGRect frame = self.frame;
    frame.size.width  = hw_size.width;
    frame.size.height = hw_size.height;
    self.frame = frame;
}

- (CGSize)hw_size{
    return self.frame.size;
}

- (CGFloat)hw_max_x{
    return CGRectGetMaxX(self.frame);
}

- (CGFloat)hw_max_y{
    return CGRectGetMaxY(self.frame);
}

- (UIView*)subViewOfClassName:(NSString*)className {
    for (UIView* subView in self.subviews) {
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        
        UIView* resultFound = [subView subViewOfClassName:className];
        if (resultFound) {
            return resultFound;
        } }
    
    return nil;
}

- (void)roundCornerRect:(CGSize)cornerSize type:(UIRectCorner)type{
    //设置所需的圆角位置以及大小
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:type cornerRadii:cornerSize];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}


@end

//=============================================================================//
//  UIColor
//=============================================================================//
@implementation UIColor (HWCategory)
+ (UIColor *)hwColor:(id)object{
    //16进制字符串
    if ([object isKindOfClass:[NSString class]]){
        return [self hwColorWithString:(NSString *)object];
    }
    //UIColor
    else if ([object isKindOfClass:[UIColor class]]){
        return (UIColor *)object;
    }
    
    return [UIColor blackColor];
}

+ (UIColor *)hwColorWithString:(NSString *)string{
    //删除字符串中的空格
    NSString *cString = [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    //长度是 6 或 8
    if ([cString length] < 6){
        return [UIColor clearColor];
    }
    
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"] || [cString hasPrefix:@"0x"]){
        cString = [cString substringFromIndex:2];
    }
    
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"]){
        cString = [cString substringFromIndex:1];
    }
    
    if ([cString length] != 6){
        return [UIColor blackColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float)r / 255.0f)
                           green:((float)g / 255.0f)
                            blue:((float)b / 255.0f)
                           alpha:1.0];
}

/*颜色设置 可调整透明度*/
+ (UIColor *)hwColor:(id)object alpha:(CGFloat)dAlpha{
    //16进制字符串
    if ([object isKindOfClass:[NSString class]]){
        return [self hwColorWithString:(NSString *)object alpha:dAlpha];
    }
    //UIColor
    else if ([object isKindOfClass:[UIColor class]]){
        return (UIColor *)object;
    }
    
    return [UIColor blackColor];
}

+ (UIColor *)hwColorWithString:(NSString *)string alpha:(CGFloat)dAlpha{
    //删除字符串中的空格
    NSString *cString = [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    //长度是 6 或 8
    if ([cString length] < 6){
        return [UIColor clearColor];
    }
    
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"] || [cString hasPrefix:@"0x"]){
        cString = [cString substringFromIndex:2];
    }
    
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"]){
        cString = [cString substringFromIndex:1];
    }
    
    if ([cString length] != 6){
        return [UIColor blackColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float)r / 255.0f)
                           green:((float)g / 255.0f)
                            blue:((float)b / 255.0f)
                           alpha:dAlpha];
}





@end

//=============================================================================//
//  UIButton
//=============================================================================//
@implementation UIButton (HWCategory)

- (UIButton *(^)(id))hw_imageUpTitleDown
{
    return ^id(id offsetY){
        CGFloat Y = 3;
        Y = [offsetY floatValue]>0?[offsetY floatValue]:3;
        [self setImageEdgeInsets:UIEdgeInsetsMake(-self.imageView.hw_h/2,0,0,-self.titleLabel.hw_w)];
        [self setTitleEdgeInsets:UIEdgeInsetsMake(self.imageView.hw_h+Y,-self.imageView.hw_w,0,0)];
        return self;
    };
}

@end
