UIView加任意边框(Masonry)

最近项目中需要给View类型加边框的实在太多了,然后就给UIView写了个类目,方便使用。不足之处请指出_

https://github.com/msbaby520/MSViewBorder
pod ‘MSViewBorder’

UIView+Extension.h

#import <UIKit/UIKit.h>
@interface UIView (Extension)
// 定义边框方向枚举
typedef NS_ENUM(NSInteger,LineDirection){
    LineDirectionTop = 1 << 0,
    LineDirectionBottom = 1 << 1,
    LineDirectionLeft = 1 << 2,
    LineDirectionRight = 1 << 3
};

/*!
 *  为本view添加边线
 *
 *  @param direction     边线方向 中间加 | 同时添加多个边框
 *  @param color         边框的颜色
 *  @param heightOrwidth 变量的宽度或者高度
 */
- (void)addLineDirection:(LineDirection)direction BackgroundColor:(UIColor *)color HeightOrWidth:(NSInteger)heightOrwidth;

@end

UIView+Extension.m

#import <Masonry.h>
#import "UIView+Extension.h"

@implementation UIView (Extension)

 1. (void)addLineDirection:(LineDirection)direction BackgroundColor:(UIColor *)color HeightOrWidth:(NSInteger)heightOrwidth{
    
    if (direction & LineDirectionTop) {
        UIView *line = [UIView new];
        line.backgroundColor = color;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self);
            make.height.equalTo(@(heightOrwidth));
        }];
    }
    
    if (direction & LineDirectionBottom) {
        UIView *line = [UIView new];
        line.backgroundColor = color;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.bottom.equalTo(self);
            make.height.equalTo(@(heightOrwidth));
        }];
    }
    
    if (direction & LineDirectionLeft) {
         UIView *line = [UIView new];
        line.backgroundColor = color;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(heightOrwidth));
            make.left.equalTo(self);
            make.top.bottom.equalTo(self);
        }];
    }
    
    if (direction & LineDirectionRight) {
        UIView *line = [UIView new];
        line.backgroundColor = color;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(heightOrwidth));
            make.right.equalTo(self);
            make.top.bottom.equalTo(self);
        }];
    }
}
@end

###如何使用?###

  1. 加单个边框
[UIView addLineDirection:(LineDirectionTop) BackgroundColor:UIColor HeightOrWidth:1];//顶部加边框
  1. 加多个边框,边框的方向用竖线隔开。
[UIView addLineDirection:(LineDirectionTop|LineDirectionBottom|  LineDirectionLeft|LineDirectionRight) BackgroundColor:UIColor HeightOrWidth:1];//上下左右加边框
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值