msbaby的博客

欢迎大家指出不足之处^_^

UIView加任意边框(Masonry)

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

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) {
        UIImageView *line = [UIImageView 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) {
        UIImageView *line = [UIImageView 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) {
        UIImageView *line = [UIImageView 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) {
        UIImageView *line = [UIImageView 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];//上下左右加边框
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010231453/article/details/51555996
个人分类: iOS
上一篇提高CocoaPods速度
下一篇iOS 隐藏下级页面的tabbar的统一处理
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭