iOS 为UIView的某个方向添加边框

释放双眼,带上耳机,听听看~!
// 添加UIView分类

// UIView+WZB.h
#import <UIKit/UIKit.h>


/**
 边框方向

 - WZBBorderDirectionTop: 顶部
 - WZBBorderDirectionLeft: 左边
 - WZBBorderDirectionBottom: 底部
 - WZBBorderDirectionRight: 右边
 */
typedef NS_ENUM(NSInteger, WZBBorderDirectionType) {
    WZBBorderDirectionTop = 0,
    WZBBorderDirectionLeft,
    WZBBorderDirectionBottom,
    WZBBorderDirectionRight
};

@interface UIView (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"

@implementation UIView (WZB)

- (void)wzb_addBorder:(WZBBorderDirectionType)direction color:(UIColor *)color width:(CGFloat)width
{
    CALayer *border = [CALayer layer];
    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.layer addSublayer:border];
}

人已赞赏
iOS文章

iOS 根据bundle中的文件名读取图片

2020-2-5 17:34:09

iOS文章

iOS 判断该图片是否有透明度通道

2020-2-5 17:56:36

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索