最近项目中需要给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
###如何使用?###
- 加单个边框
[UIView addLineDirection:(LineDirectionTop) BackgroundColor:UIColor HeightOrWidth:1];//顶部加边框
- 加多个边框,边框的方向用竖线隔开。
[UIView addLineDirection:(LineDirectionTop|LineDirectionBottom| LineDirectionLeft|LineDirectionRight) BackgroundColor:UIColor HeightOrWidth:1];//上下左右加边框