美文网首页
iOS中实现上下左右边框的自定义显示

iOS中实现上下左右边框的自定义显示

作者: 841883f31e69 | 来源:发表于2016-09-05 15:12 被阅读442次

    有时候我们需要自定义上下左右边框哪一边或几边显示,现在介绍下我的方法,希望可以为大家提供一种思路。

    项目中调用类似:

    self.view.borderWhich = ZJViewBorderBottom;

    不说那么多,直接上代码。

    UIView+additional.h

    #import

    typedef NS_ENUM(NSInteger, ZJViewBorder) {

    ZJViewBorderTop = 1<<1,

    ZJViewBorderLeft = 1<<2,

    ZJViewBorderBottom = 1<<3,

    ZJViewBorderRight = 1<<4,

    };

    @interface UIView (additional)

    @property (nonatomic, assign) ZJViewBorder borderWhich;

    @end

    UIView+additional.m

    #import "UIView+additional.h"

    @implementation UIView (additional)

    @dynamic borderWhich;

    - (void)setBorderWhich:(ZJViewBorder)borderWhich {

    CGFloat bh = self.layer.borderWidth;

    if (borderWhich & ZJViewBorderBottom) {

    [self addBottomBorder:self borderHeight:bh];

    }

    if (borderWhich & ZJViewBorderLeft) {

    [self addLeftBorder:self borderHeight:bh];

    }

    if (borderWhich & ZJViewBorderRight) {

    [self addRightBorder:self borderHeight:bh];

    }

    if (borderWhich & ZJViewBorderTop) {

    [self addTopBorder:self borderHeight:bh];

    }

    self.layer.borderWidth = 0;

    }

    - (void)addTopBorder:(UIView *)vi borderHeight:(CGFloat)bh {

    CGColorRef col = vi.layer.borderColor;

    if (vi.layer.borderWidth > 1000 || vi.layer.borderWidth == 0) {

    bh = 1;

    }

    else

    bh = vi.layer.borderWidth;

    CALayer *border = [CALayer layer];

    border.frame = CGRectMake(0, 0, vi.frame.size.width, bh);

    border.backgroundColor = col;

    [vi.layer addSublayer:border];

    }

    - (void)addLeftBorder:(UIView *)vi borderHeight:(CGFloat)bh{

    CGColorRef col = vi.layer.borderColor;

    if (vi.layer.borderWidth > 1000 || vi.layer.borderWidth == 0) {

    bh = 1;

    }

    else

    bh = vi.layer.borderWidth;

    CALayer *border = [CALayer layer];

    border.frame = CGRectMake(0, 0, bh, vi.frame.size.height);

    border.backgroundColor = col;

    [vi.layer addSublayer:border];

    }

    - (void)addBottomBorder:(UIView *)vi borderHeight:(CGFloat)bh{

    CGColorRef col = vi.layer.borderColor;

    if (vi.layer.borderWidth > 1000 || vi.layer.borderWidth == 0) {

    bh = 1;

    }

    else

    bh = vi.layer.borderWidth;

    CALayer *border = [CALayer layer];

    border.frame = CGRectMake(0, vi.frame.size.height-bh, vi.frame.size.width, bh);

    border.backgroundColor = col;

    [vi.layer addSublayer:border];

    }

    - (void)addRightBorder:(UIView *)vi borderHeight:(CGFloat)bh{

    CGColorRef col = vi.layer.borderColor;

    if (vi.layer.borderWidth > 1000 || vi.layer.borderWidth == 0) {

    bh = 1;

    }

    else

    bh = vi.layer.borderWidth;

    CALayer *border = [CALayer layer];

    border.frame = CGRectMake(vi.frame.size.width-bh, 0, bh, vi.frame.size.height);

    border.backgroundColor = col;

    [vi.layer addSublayer:border];

    }

    @end

    相关文章

      网友评论

          本文标题:iOS中实现上下左右边框的自定义显示

          本文链接:https://www.haomeiwen.com/subject/nnprettx.html