OC 版本
.h 文件
#import <UIKit/UIKit.h>
typedef NS_OPTIONS(NSUInteger, UIBorderSideType) {
UIBorderSideTypeAll = 0,
UIBorderSideTypeTop = 1 << 0,
UIBorderSideTypeBottom = 1 << 1,
UIBorderSideTypeLeft = 1 << 2,
UIBorderSideTypeRight = 1 << 3,
};
@interface UIView (BorderLine)
- (UIView *)borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType;
@end
.m文件
#import "UIView+BorderLine.h"
@implementation UIView (BorderLine)
- (UIView *)borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
if (borderType == UIBorderSideTypeAll) {
self.layer.borderWidth = borderWidth;
self.layer.borderColor = color.CGColor;
return self;
}
/// 左侧
if (borderType & UIBorderSideTypeLeft) {
/// 左侧线路径
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f) toPoint:CGPointMake(0.0f, self.frame.size.height) color:color borderWidth:borderWidth]];
}
/// 右侧
if (borderType & UIBorderSideTypeRight) {
/// 右侧线路径
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
}
/// top
if (borderType & UIBorderSideTypeTop) {
/// top线路径
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.frame.size.width, 0.0f) color:color borderWidth:borderWidth]];
}
/// bottom
if (borderType & UIBorderSideTypeBottom) {
/// bottom线路径
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, self.frame.size.height) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
}
return self;
}
- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
/// 线的路径
UIBezierPath * bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:p0];
[bezierPath addLineToPoint:p1];
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = color.CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
/// 添加路径
shapeLayer.path = bezierPath.CGPath;
/// 线宽度
shapeLayer.lineWidth = borderWidth;
return shapeLayer;
}
@end
swift 版本
import UIKit
import Foundation
struct UIBorderSideType: OptionSet {
var rawValue: Int
init(rawValue: Int) {
self.rawValue = rawValue
}
static var UIBorderSideTypeAll: UIBorderSideType = UIBorderSideType(rawValue: 0)
static var UIBorderSideTypeLeft: UIBorderSideType = UIBorderSideType(rawValue: 1 << 0)
static var UIBorderSideTypeRight: UIBorderSideType = UIBorderSideType(rawValue: 1 << 1)
static var UIBorderSideTypeTop: UIBorderSideType = UIBorderSideType(rawValue: 1 << 2)
static var UIBorderSideTypeBottom: UIBorderSideType = UIBorderSideType(rawValue: 1 << 3)
}
extension UIView {
@discardableResult
func border(color: UIColor, width: CGFloat, type: UIBorderSideType) -> Self {
if type.contains(UIBorderSideType.UIBorderSideTypeAll) {
self.layer.borderColor = color.cgColor
self.layer.borderWidth = width
return self
}
if type.contains(UIBorderSideType.UIBorderSideTypeLeft) {
self.layer.addSublayer(self.addLine(originPoint: CGPoint.zero, toPoint: CGPoint(x: 0, y: self.frame.size.height), color: color, width: width))
}
if type.contains(UIBorderSideType.UIBorderSideTypeRight) {
self.layer.addSublayer(self.addLine(originPoint: CGPoint(x: self.frame.size.width, y: 0), toPoint: CGPoint(x: self.frame.size.width, y: self.frame.size.height), color: color, width: width))
}
if type.contains(UIBorderSideType.UIBorderSideTypeTop) {
self.layer.addSublayer(self.addLine(originPoint: CGPoint(x: 0, y: 0), toPoint: CGPoint(x: self.frame.size.width, y: 0), color: color, width: width))
}
if type.contains(UIBorderSideType.UIBorderSideTypeBottom) {
self.layer.addSublayer(self.addLine(originPoint: CGPoint(x: 0, y: self.frame.size.height), toPoint: CGPoint(x: self.frame.size.width, y: self.frame.size.height), color: color, width: width))
}
return self
}
func addLine(originPoint: CGPoint, toPoint: CGPoint, color: UIColor, width: CGFloat) -> CAShapeLayer {
let bezierPath = UIBezierPath()
bezierPath.move(to: originPoint)
bezierPath.addLine(to: toPoint)
let shapeLayer = CAShapeLayer()
// 线宽度
shapeLayer.lineWidth = width;
shapeLayer.strokeColor = color.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
// 添加路径
shapeLayer.path = bezierPath.cgPath
return shapeLayer
}
}
网友评论