示例代码

//

#import <UIKit/UIKit.h>

@interface UIView (LPCView)

/** 上 */
@property CGFloat top;
/** 下 */
@property CGFloat bottom;
/** 左 */
@property CGFloat left;
/** 右 */
@property CGFloat right;
/** 中心x */
@property CGFloat centerX;
/** 中心y */
@property CGFloat centerY;
/** 高 */
@property CGFloat height;
/** 宽 */
@property CGFloat width;
/** 角标数 */
@property NSInteger brigeNum;
/** 尺寸 */
@property CGSize size;
/** 坐标 */
@property (nonatomic, assign) CGPoint origin;
/** 偏移x */
@property (nonatomic, assign) CGFloat tx;
/** 偏移y */
@property (nonatomic, assign) CGFloat ty;
/** 是否圆角-高的十分之一 */
@property BOOL isCornerRadiusHeight10;
/** 是否切圆-高的一半 */
@property BOOL isCornerRadiusHalfWidth;
/** 边框颜色 */
@property (nonatomic, strong) UIColor * borderColor;
/** 虚线边框 */
- (void)LPCDashedLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth spaceAry:(NSArray<NSNumber *> *)spaceAry;
/** 实线边框 */
- (void)LPCSolidLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth;
/** 点击事件-目标-事件 */
- (void)addTarget:(id)target action:(SEL)action;
/** 当前View的视图控制器 */
- (nullable UIViewController *)viewController;

NS_ASSUME_NONNULL_BEGIN
//__nullable表示对象可以是NULL或nil,而__nonnull表示对象不应该为空
/** 避免出现警告 */
NS_ASSUME_NONNULL_END

@end
////////////////////////////m//////////////////////////

//

#import "UIView+LPCView.h"

@implementation UIView (LPCView)

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

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

- (CGFloat)bottom {
    return self.top + self.height;
}

- (void)setBottom:(CGFloat)bottom {
    self.top = bottom - self.height;
}

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

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

- (CGFloat)right {
    return self.left + self.width;
}

- (void)setRight:(CGFloat)right {
    self.left = right - self.width;
}

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

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.centerY);
}

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

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.centerX, centerY);
}

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

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = 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;
}
- (CGSize)size {
    return self.frame.size;
}
- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

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

- (void)setOrigin:(CGPoint)origin {
    self.frame = CGRectMake(origin.x, origin.y, self.width, self.height);
}

- (CGFloat)tx {
    return self.transform.tx;
}

- (void)setTx:(CGFloat)tx {
    CGAffineTransform transform = self.transform;
    transform.tx = tx;
    self.transform = transform;
}

- (CGFloat)ty {
    return self.transform.ty;
}

- (void)setTy:(CGFloat)ty {
    CGAffineTransform transform = self.transform;
    transform.ty = ty;
    self.transform = transform;
}

- (nullable UIViewController *)viewController {
    UIResponder *responder = self;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: [UIViewController class]]) {
            return (UIViewController *)responder;
        }
    return nil;
}

//角标
- (NSInteger)brigeNum {
    if ([[self viewWithTag:48511] isKindOfClass:[UILabel class]]) {
        UILabel * tempLabel = [self viewWithTag:48511];
        return [tempLabel.text integerValue];
    }
    return 0;
}
//角标
- (void)setBrigeNum:(NSInteger)brigeNum {
    NSInteger length = [NSString stringWithFormat:@"%ld", (long)brigeNum].length;
    brigeNum = brigeNum > 99 ? 99 : brigeNum;
    if (0 == length && 0 == brigeNum) {
        return;
    }
    CGRect tempFrame = CGRectMake(self.left + self.width - 6,self.top, 12, 12);
    if ([[self.superview viewWithTag:48511] isKindOfClass:[UILabel class]]) {
        UILabel * tempLabel = [self.superview viewWithTag:48511];
        tempLabel.frame = tempFrame;
        tempLabel.text = [NSString stringWithFormat:@"%ld", (long)brigeNum];
        if (0 == brigeNum) {
            tempLabel.alpha = 0;
        }else {
            tempLabel.alpha = 1;
        }
    }else {
        UILabel * la = [[UILabel alloc] initWithFrame:tempFrame];
        la.text = [NSString stringWithFormat:@"%ld", (long)brigeNum];
        la.font = [UIFont systemFontOfSize:9];
        la.textAlignment = NSTextAlignmentCenter;
        la.textColor = [UIColor whiteColor];
        la.layer.cornerRadius = 6;
        la.layer.masksToBounds = YES;
        
        la.tag = 48511;
        la.backgroundColor = [UIColor redColor];
        
        if (length > 2) {
            la.frame = CGRectMake(tempFrame.size.width - 6, tempFrame.origin.y, 12, 12);
        }
        [self.superview addSubview:la];
        if (0 == brigeNum) {
            la.alpha = 0;
        }else {
            la.alpha = 1;
        }
    }
}

//边框颜色
- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}
- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = [borderColor CGColor];
}
//圆角度
- (BOOL)isCornerRadiusHeight10 {
    if (self.height / 10.0 == self.layer.cornerRadius) {
        return YES;
    }
    return NO;
}

- (void)setIsCornerRadiusHeight10:(BOOL)isCornerRadiusHeight10 {
    if (isCornerRadiusHeight10) {
        self.layer.cornerRadius = self.height * kScale_Height / 10.0;
        self.layer.masksToBounds = YES;
    }else {
        isCornerRadiusHeight10 = NO;
    }
}
//切圆
- (BOOL)isCornerRadiusHalfWidth {
    if (self.layer.cornerRadius == self.height / 2.0) {
        return YES;
    }
    return NO;
}

- (void)setIsCornerRadiusHalfWidth:(BOOL)isCornerRadiusHalfWidth {
    if (isCornerRadiusHalfWidth) {
        self.layer.cornerRadius = self.height / 2.0;
        self.layer.masksToBounds = YES;
    }else {
        isCornerRadiusHalfWidth = NO;
    }
}

//虚线边框
- (void)LPCDashedLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth spaceAry:(NSArray<NSNumber *> *)spaceAry {
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    
    borderLayer.bounds = CGRectMake(0, 0, self.width , self.height);
    
    borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
    //不带圆角
//    borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;
    //带圆角
    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    borderLayer.lineWidth = lineWidth / [[UIScreen mainScreen] scale];
    //虚线边框
    borderLayer.lineDashPattern = spaceAry;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = lineColor.CGColor;
    [self.layer addSublayer:borderLayer];
}
//实线变框
- (void)LPCSolidLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth {
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    
    borderLayer.bounds = CGRectMake(0, 0, self.width , self.height);
    
    borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
    //不带圆角
    //    borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;
    //带圆角
    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    borderLayer.lineWidth = lineWidth / [[UIScreen mainScreen] scale];
    //实线边框
    borderLayer.lineDashPattern = nil;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = lineColor.CGColor;
    [self.layer addSublayer:borderLayer];
}

/** 点击事件-目标-事件 */
- (void)addTarget:(id)target action:(SEL)action {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}

@end

最新文章

  1. ubuntu下载工具uget和aria2
  2. Js获取下拉框选定项的值和文本
  3. 在Mysql中如何显示所有用户?
  4. 回文串+回溯法 URAL 1635 Mnemonics and Palindromes
  5. NAT后面的FTP SERVER终极篇
  6. GPS获取Location 获取所在地点的经纬度
  7. Python Lib:pyzmq
  8. python从入门到实践-4章操作列表
  9. jmeter接口入门操作手册
  10. react项目中实现悬浮(hover)在按钮上时在旁边显示提示
  11. Wowza 相关
  12. oracle数据文件迁移
  13. nginx转发
  14. Foxpro数据库连接错误解决方法--【VFP DBF文件不是一个有效的路径。 确定路径名称拼写是否正确,以及是否连接到文件存放的服务器】
  15. topcoder srm 555 div1
  16. wdatepicker默认时间为当前时间
  17. 6. Manage the driver for browser and the script for Hub
  18. js数组的forEach方法能不能修改数组的值
  19. 【laravel】passport的scope作用域
  20. 利用Fiddler或Charles进行mock数据创造测试环境

热门文章

  1. 机器指令翻译成 JavaScript —— No.3 流程分割
  2. Javascript高级技巧
  3. 全网络最正确的让 Linux 开机进入字符界面的方法及设置 FrameBuffer 分辨率的方法
  4. 感悟 GNU C 以及将 Vim 打造成 C/C++ 的半自动化 IDE
  5. 页面元素坐标和偏移(clientX/pageX/screenX/layerX/offsetWidth/scrollWidth/clientWidth等)相关整理
  6. 周六搞事情,微信小程序开发文档已放出!
  7. Maven pom文件常用配置,转载
  8. Atitit 图像处理和计算机视觉的分类&#160;三部分&#160;图像处理&#160;图像分析&#160;计算机视觉
  9. clearfix的最佳方案----在路上(22)
  10. form表单 ----在路上(15)