关闭

UIView加任意边框(Masonry)

标签: uiview界面边框任意
241人阅读 评论(0) 收藏 举报
分类:

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

UIView+Extension.h

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

/*!
 *  为本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];//上下左右加边框
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17217次
    • 积分:428
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条