本文主要是介绍[iOS]UIView+BorderLine,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
经常遇到给UIView添加边框的需求,最近看见以前别人项目中写的这种方式比较好用,这里也备个份.
- (void)viewDidLoad {[super viewDidLoad];[_view1 addBorderLineWithBorderWidth:1 borderColor:[UIColor redColor] cornerRadius:10];[_view2 addBorderLineWithBorderWidth:1 borderColor:[UIColor redColor] cornerRadius:10 borderType:UIBorderSideTypeTop];[_view3 addBorderLineWithBorderWidth:1 borderColor:[UIColor redColor] cornerRadius:10 borderType:UIBorderSideTypeTop];[_view3 addBorderLineWithBorderWidth:1 borderColor:[UIColor redColor] cornerRadius:10 borderType:UIBorderSideTypeLeft];[_view4 addBorderLineWithBorderWidth:2 borderColor:[UIColor redColor] cornerRadius:10 borderType:UIBorderSideTypeLeft];[_view4.layer setMasksToBounds:YES];[_view4.layer setCornerRadius:10];
}
UIView+BorderLine
资源下载
#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)/**增加边框@param borderWidth 边框宽度@param borderColor 边框颜色@param cornerRadius 边框圆角*/
- (void)addBorderLineWithBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)cornerRadius;/**增加边框@param borderWidth 边框宽度@param borderColor 边框颜色@param cornerRadius 边框圆角@param borderType 边框类型*/
- (void)addBorderLineWithBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)cornerRadius borderType:(UIBorderSideType)borderType;@end
#import "UIView+BorderLine.h"@implementation UIView (BorderLine)- (void)addBorderLineWithBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)cornerRadius borderType:(UIBorderSideType)borderType {CGFloat space = borderWidth / 2;switch (borderType) {case UIBorderSideTypeAll: {CAShapeLayer *lineBorder = [[CAShapeLayer alloc] init];lineBorder.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);[lineBorder setLineWidth:borderWidth];[lineBorder setStrokeColor:borderColor.CGColor];[lineBorder setFillColor:[UIColor clearColor].CGColor];UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:lineBorder.frame byRoundingCorners:UIRectCornerAllCorners cornerRadii:(CGSizeMake(cornerRadius, cornerRadius))];lineBorder.path = path.CGPath;[self.layer addSublayer:lineBorder];}break;case UIBorderSideTypeLeft: {[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f + space) toPoint:CGPointMake(0.0f, self.frame.size.height - space) color:borderColor borderWidth:borderWidth]];}break;case UIBorderSideTypeRight: {[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f + space) toPoint:CGPointMake(self.frame.size.width, self.frame.size.height - space) color:borderColor borderWidth:borderWidth]];}break;case UIBorderSideTypeTop: {[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f + space, 0.0f) toPoint:CGPointMake(self.frame.size.width - space, 0.0f) color:borderColor borderWidth:borderWidth]];}break;case UIBorderSideTypeBottom: {[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f + space, self.frame.size.height) toPoint:CGPointMake(self.frame.size.width - space, self.frame.size.height) color:borderColor borderWidth:borderWidth]];}break;default:break;}
}- (void)addBorderLineWithBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)cornerRadius {[self addBorderLineWithBorderWidth:borderWidth borderColor:borderColor cornerRadius:cornerRadius borderType:UIBorderSideTypeAll];
}- (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
这篇关于[iOS]UIView+BorderLine的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!