美文网首页
iOS 设置View的单个边的Border

iOS 设置View的单个边的Border

作者: wodeph | 来源:发表于2023-12-07 14:11 被阅读0次

    #import <UIKit/UIKit.h>

    NS_ASSUME_NONNULL_BEGIN

    @interfaceUIView(GJWUnilateralBorder)

    @property (nonatomic) CGFloat height;

    @property (nonatomic) CGFloat width;

    -(void)setViewBorderWithTop:(BOOL)top

                       Left:(BOOL)left

                     Bottom:(BOOL)bottom

                      Right:(BOOL)right

                BorderColor:(UIColor*)borderColor

                BorderWidth:(CGFloat)borderWidth;

    @end

    NS_ASSUME_NONNULL_END

    #import "UIView+GJWUnilateralBorder.h"

    @implementationUIView(GJWUnilateralBorder)

    - (void)setHeight:(CGFloat)height{

        CGRectframe =self.frame;

        frame.size.height= height;

        self.frame= frame;

    }

    - (CGFloat)height{

        return self.frame.size.height;

    }

    - (void)setWidth:(CGFloat)width{

        CGRectframe =self.frame;

        frame.size.width= width;

        self.frame= frame;

    }

    - (CGFloat)width{

        return self.frame.size.width;

    }

    -(void)setViewBorderWithTop:(BOOL)topLeft:(BOOL)leftBottom:(BOOL)bottomRight:(BOOL)rightBorderColor:(UIColor*)borderColorBorderWidth:(CGFloat)borderWidth{

        if(top) {

            CALayer*topBorder = [CALayerlayer];

            topBorder.frame=CGRectMake(0,0,self.width, borderWidth *1.5);

            topBorder.backgroundColor= borderColor.CGColor;

            [self.layeraddSublayer:topBorder];

        }

        if(left) {

            CALayer*leftBorder = [CALayerlayer];

            leftBorder.frame=CGRectMake(0,0, borderWidth *1.5,self.height);

            leftBorder.backgroundColor= borderColor.CGColor;

            [self.layeraddSublayer:leftBorder];

        }

        if(bottom) {

            CALayer*bottomBorder = [CALayerlayer];

            bottomBorder.frame=CGRectMake(0,self.height- borderWidth *1.5,self.width, borderWidth *1.5);

            bottomBorder.backgroundColor= borderColor.CGColor;

            [self.layeraddSublayer:bottomBorder];

        }

        if(right) {

            CALayer*rightBorder = [CALayerlayer];

            rightBorder.frame=CGRectMake(self.width- borderWidth *1.5,0, borderWidth *1.5,self.height);

            rightBorder.backgroundColor= borderColor.CGColor;

            [self.layeraddSublayer:rightBorder];

        }

    }

    相关文章

      网友评论

          本文标题:iOS 设置View的单个边的Border

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