(IOS)为UIView的某个方向添加边框

2018-08-10  本文已影响0人  rightmost

 // 添加UIView分类

    // UIView+WZB.h

#import

    /**

     边框方向

     - WZBBorderDirectionTop: 顶部

     - WZBBorderDirectionLeft: 左边

     - WZBBorderDirectionBottom: 底部

     - WZBBorderDirectionRight: 右边

     */

    typedefNS_ENUM(NSInteger, WZBBorderDirectionType) {

        WZBBorderDirectionTop =0,

        WZBBorderDirectionLeft,

        WZBBorderDirectionBottom,

        WZBBorderDirectionRight

    };

    @interfaceUIView (WZB)

    /**

     为UIView的某个方向添加边框

     @param direction 边框方向

     @param color 边框颜色

     @param width 边框宽度

     */

    - (void)wzb_addBorder:(WZBBorderDirectionType)direction color:(UIColor*)color width:(CGFloat)width;

    @end

    // UIView+WZB.m

#import "UIView+WZB.h"

    @implementationUIView (WZB)

    - (void)wzb_addBorder:(WZBBorderDirectionType)direction color:(UIColor*)color width:(CGFloat)width

    {

        CALayer*border = [CALayerlayer];

        border.backgroundColor= color.CGColor;

        switch(direction) {

            case WZBBorderDirectionTop:

            {

                border.frame=CGRectMake(0.0f,0.0f,self.bounds.size.width, width);

            }

                break;

            case WZBBorderDirectionLeft:

            {

                border.frame=CGRectMake(0.0f,0.0f, width,self.bounds.size.height);

            }

                break;

            case WZBBorderDirectionBottom:

            {

                border.frame=CGRectMake(0.0f,self.bounds.size.height- width,self.bounds.size.width, width);

            }

                break;

            case WZBBorderDirectionRight:

            {

                border.frame=CGRectMake(self.bounds.size.width- width,0, width,self.bounds.size.height);

            }

                break;

            default:

                break;

        }

        [self.layeraddSublayer:border];

    }

上一篇 下一篇

猜你喜欢

热点阅读