// // UIView+Extension.m // JWlive // // Created by pican zhang on 16/3/17. // Copyright © 2016年 pican zhang. All rights reserved. // #import "UIView+Extension.h" @implementation UIView (Extension) - (void)setSize:(CGSize)size { CGRect frame = self.frame; frame.size = size; self.frame = frame; } - (CGSize)size { return self.frame.size; } - (void)setX:(CGFloat)x { CGRect frame = self.frame; frame.origin.x = x; self.frame = frame; } - (CGFloat)x { return self.frame.origin.x; } - (void)setY:(CGFloat)y { CGRect frame = self.frame; frame.origin.y = y; self.frame = frame; } - (CGFloat)y { return self.frame.origin.y; } - (void)setWidth:(CGFloat)width { CGRect frame = self.frame; frame.size.width = width; self.frame = frame; } - (CGFloat)width { return self.frame.size.width; } - (void)setHeight:(CGFloat)height { CGRect frame = self.frame; frame.size.height = height; self.frame = frame; } - (CGFloat)height { return self.frame.size.height; } - (void)setCenterX:(CGFloat)centerX { CGPoint center = self.center; center.x = centerX; self.center = center; } - (CGFloat)centerX { return self.center.x; } - (void)setCenterY:(CGFloat)centerY { CGPoint center = self.center; center.y = centerY; self.center = center; } - (CGFloat)centerY { return self.center.y; } - (void)setOrigin:(CGPoint)origin { CGRect frame = self.frame; frame.origin = origin; self.frame = frame; } - (CGPoint)origin { return self.frame.origin; } - (void)HorizonLinePaddingToTop:(CGFloat)top{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(0, top, self.frame.size.width, 0.5); line.backgroundColor = HexColorFromRGB(0xe6e6e6); [self addSubview:line]; } - (void)VerticalLinePaddingToLeft:(CGFloat)left{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(left, 0, 0.5, self.frame.size.height); line.backgroundColor = HexColorFromRGB(0xe6e6e6); [self addSubview:line]; } - (void)RightLinePaddingTop:(CGFloat)top{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(self.frame.size.width-0.5, top, 0.5, self.frame.size.height-2*top); line.backgroundColor = [UIColor whiteColor]; [self addSubview:line]; } - (void)LeftLinePaddingTop:(CGFloat)top{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(0, top, 0.5, self.frame.size.height-2*top); line.backgroundColor = HexColorFromRGB(0xe6e6e6); [self addSubview:line]; } - (void)topLineforView{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(0, 0, KScreenWidth, 0.5); line.backgroundColor = HexColorFromRGB(0xe6e6e6); [self addSubview:line]; } - (void)topLineforViewWithColor:(UIColor*)color{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(0, 0, KScreenWidth, 0.5); line.backgroundColor = color; [self addSubview:line]; } - (void)topLinepaddingLeft:(CGFloat)left{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(left, 0, KScreenWidth-left, 0.5); line.backgroundColor = HexColorFromRGB(0xe6e6e6); [self addSubview:line]; } - (void)bottomLineforViewHeight:(CGFloat)height{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(0, height-0.5, KScreenWidth, 0.5); line.backgroundColor = LZF5F4F7Color; [self addSubview:line]; } - (void)bottomLineforView{ // UIView* line = [[UIView alloc]init]; // line.frame = CGRectMake(0, self.height-1.0, KScreenWidth, 1.0); // line.backgroundColor = HexColorFromRGB(0xe6e6e6); // [self addSubview:line]; } - (void)bottomLineforViewWithColor:(UIColor*)color{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(0, self.frame.size.height-0.5, self.frame.size.width, 0.5); NSLog(@"%@",NSStringFromCGRect(line.frame)); line.backgroundColor =color; [self addSubview:line]; } - (void)bottomWideLineforView:(CGFloat)height{ UIView* line = [[UIView alloc]init]; line.frame = CGRectMake(0, self.frame.size.height-height, self.frame.size.width, height); line.backgroundColor = HexColorFromRGB(0xe6e6e6); [self addSubview:line]; } - (void)addLineWithColor:(UIColor *)color lineRect:(CGRect)rect{ UIView* line = [[UIView alloc]init]; line.frame = rect; line.backgroundColor = color; [self addSubview:line]; } - (UIViewController *)viewController{ for (UIView* next = self; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) { return (UIViewController*)nextResponder; } } return nil; } - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertPoint:point toWindow:nil]; } else { return [self convertPoint:point toView:nil]; } } UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; if ((!from || !to) || (from == to)) return [self convertPoint:point toView:view]; point = [self convertPoint:point toView:from]; point = [to convertPoint:point fromWindow:from]; point = [view convertPoint:point fromView:to]; return point; } - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertPoint:point fromWindow:nil]; } else { return [self convertPoint:point fromView:nil]; } } UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; if ((!from || !to) || (from == to)) return [self convertPoint:point fromView:view]; point = [from convertPoint:point fromView:view]; point = [to convertPoint:point fromWindow:from]; point = [self convertPoint:point fromView:to]; return point; } - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertRect:rect toWindow:nil]; } else { return [self convertRect:rect toView:nil]; } } UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; if (!from || !to) return [self convertRect:rect toView:view]; if (from == to) return [self convertRect:rect toView:view]; rect = [self convertRect:rect toView:from]; rect = [to convertRect:rect fromWindow:from]; rect = [view convertRect:rect fromView:to]; return rect; } - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertRect:rect fromWindow:nil]; } else { return [self convertRect:rect fromView:nil]; } } UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; if ((!from || !to) || (from == to)) return [self convertRect:rect fromView:view]; rect = [from convertRect:rect fromView:view]; rect = [to convertRect:rect fromWindow:from]; rect = [self convertRect:rect fromView:to]; return rect; } @end