通过此方法可以设置任何控件(UIView的子类)的任意一个方向的圆角
效果如图:
上边两个角是圆角的输入框
下边两个角是圆角的输入框
当然,你也可以其他方法实现,比如用一个四个角圆角的view作为背景,然后里面装两个透明的textfield,中间画一条线,但是这种方法稍微有点耗内存,定向圆角是直接对layer进行操作,更加轻量级,所以建议用定向圆角的方法
代码:
如果使用系统的API是办不到的,只能同时设置四个角的圆角,这里我讲介绍一种办法,可以设置定向圆角
使用方法:
//1.导入头文件
#import "ZHRoundView.h"```
//2.让你的view/label/button...继承自ZHRoundView
//3.只需要一句代码就可搞定
//设置左上角圆角
self.myView.roundType = ZHRoundTypeLeft;
//设置上面两个角是圆角
//self.myView.roundType = ZHRoundTypeTop;
如果想绘制边框
view.borderWidth = 2;
view.borderColor = [UIColor blackColor].CGColor;
view.cornerRadius = 10;
源码:
头文件:ZHRoundView.h
import <UIKit/UIKit.h>
typedef enum{
ZHRoundTypeTop = 0,
ZHRoundTypeLeft,
ZHRoundTypeRight,
ZHRoundTypeBottom,
ZHRoundTypeAll,
ZHRoundTypeNone
}ZHRoundType;
@interface ZHRoundView : UIView
/**
- 圆角类型
*/
@property(nonatomic,assign)ZHRoundType roundType;
/**
- 边框宽度
*/
@property (nonatomic ,assign) CGFloat borderWidth;
/**
- 边框颜色
/
@property (nonatomic ,assign) CGColorRef borderColor;
/* - 圆角的半径
*/
@property (nonatomic ,assign) CGFloat cornerRadius;
@end
.m文件
ZHRoundView.m
//
// ZHRoundView.m
// 核心动画
//
// Created by 演员新之助 on 15/12/29.
// Copyright © 2015年 演员新之助. All rights reserved.
//
import "ZHRoundView.h"
@interface ZHRoundView()
@end
@implementation ZHRoundView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
[self setup];
return [super initWithFrame:frame];
}
-(void)awakeFromNib
{
[self setup];
}
/**
- 初始化
*/
-(void)setup{
//默认cornerRadius为10
_cornerRadius = 10;
}
/**
设置圆角方向(设置边框颜色和宽度应放在此方法之前)
-
@param roundType 定向圆角的方向
*/
-(void)setRoundType:(ZHRoundType)roundType{_roundType = roundType;
self.layer.mask = nil;
UIRectCorner corners;
switch (roundType) {
case ZHRoundTypeLeft:
corners = UIRectCornerTopLeft;
break;
case ZHRoundTypeRight:
corners = UIRectCornerBottomRight | UIRectCornerTopRight;
break;
case ZHRoundTypeBottom:
corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
break;
case ZHRoundTypeTop:
corners = UIRectCornerTopRight | UIRectCornerTopLeft;
break;
case ZHRoundTypeNone:
corners = UIRectCornerBottomLeft & UIRectCornerBottomRight;
break;
case ZHRoundTypeAll:
corners = UIRectCornerAllCorners;
break;
default:
break;
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
//绘制边框
CAShapeLayer *strokeLayer = [CAShapeLayer layer];
strokeLayer.path = maskPath.CGPath;
strokeLayer.fillColor = [UIColor clearColor].CGColor;
strokeLayer.strokeColor = _borderColor;
strokeLayer.lineWidth = _borderWidth;
// Transparent view that will contain the stroke layer
UIView *strokeView = [[UIView alloc] initWithFrame:self.bounds];
strokeView.userInteractionEnabled = NO; // in case your container view contains controls
[strokeView.layer addSublayer:strokeLayer];
// configure and add any subviews to the container view
// stroke view goes in last, above all the subviews
[self addSubview:strokeView];
}
@end
PS:源码是在群里大神"恒总"的基础上改进哒╭(╯3╰)╮,他的github地址:https://github.com/iPermanent