android控件圆角工具类,工具类(为控件设置圆角)

为了便于日常开发效率,因此创建了一些小的工具类便于使用.

具体 code 如下:

声明:

/*

为控件添加边框样式_工具类

*/

#import

typedef NS_ENUM(NSInteger,LQQSideType) {

kLQQSideTypeTop = 0,

kLQQSideTypeLeft = 1,

kLQQSideTypeBottom = 2,

kLQQSideTypeRight = 3,

kLQQSideTypeAll = 4,

};

typedef NS_ENUM(NSInteger,LQQSideAngleType) {

kLQQSideAngleTypeTopLeft = 0,

kLQQSideAngleTypeTopRight = 1,

kLQQSideAngleTypeBottomLeft = 2,

kLQQSideAngleTypeBottomRight = 3,

kLQQSideAngleTypeAll = 4,

};

@interface UIView (FYH)

/**

设置不同边的圆角

@param sideType 圆角类型

@param cornerRadius 圆角半径

*/

- (void)cornerSideType:(LQQSideType)sideType withCornerRadius:(CGFloat)cornerRadius;

/**

设置不同角的圆角

@param sideType 圆角类型

@param cornerRadius 圆角半径

*/

- (void)cornerSideAngleType:(LQQSideAngleType)sideType withCornerRadius:(CGFloat)cornerRadius;

/**

设置view某一边框

@param sideType 哪个边

@param color 边框颜色

@param width 边框宽度

*/

- (void)cornerSideType:(LQQSideType)sideType lineColor:(UIColor *)color lineWidth:(CGFloat)width;

@end

实现:

#import "UIView+FYH.h"

@implementation UIView (FYH)

- (void)cornerSideType:(LQQSideType)sideType withCornerRadius:(CGFloat)cornerRadius

{

CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);

UIBezierPath *maskPath;

switch (sideType) {

case kLQQSideTypeTop:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)

cornerRadii:cornerSize];

}

break;

case kLQQSideTypeLeft:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomLeft)

cornerRadii:cornerSize];

}

break;

case kLQQSideTypeBottom:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)

cornerRadii:cornerSize];

}

break;

case kLQQSideTypeRight:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight)

cornerRadii:cornerSize];

}

break;

default:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:UIRectCornerAllCorners

cornerRadii:cornerSize];

}

break;

}

// Create the shape layer and set its path

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = self.bounds;

maskLayer.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer

self.layer.mask = maskLayer;

[self.layer setMasksToBounds:YES];

}

- (void)cornerSideAngleType:(LQQSideAngleType)sideType withCornerRadius:(CGFloat)cornerRadius

{

CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);

UIBezierPath *maskPath;

switch (sideType) {

case kLQQSideAngleTypeTopLeft:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerTopLeft)

cornerRadii:cornerSize];

}

break;

case kLQQSideAngleTypeTopRight:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerTopRight)

cornerRadii:cornerSize];

}

break;

case kLQQSideAngleTypeBottomLeft:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerBottomLeft)

cornerRadii:cornerSize];

}

break;

case kLQQSideAngleTypeBottomRight:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:(UIRectCornerBottomRight)

cornerRadii:cornerSize];

}

break;

default:

{

maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

byRoundingCorners:UIRectCornerAllCorners

cornerRadii:cornerSize];

}

break;

}

// Create the shape layer and set its path

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = self.bounds;

maskLayer.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer

self.layer.mask = maskLayer;

[self.layer setMasksToBounds:YES];

}

- (void)cornerSideType:(LQQSideType)sideType lineColor:(UIColor *)color lineWidth:(CGFloat)width

{

CAShapeLayer *layer = [CAShapeLayer layer];

UIBezierPath *aPath = [UIBezierPath bezierPath];

switch (sideType) {

case kLQQSideTypeTop:

{

[aPath moveToPoint:CGPointMake(0.0, 0.0)];

[aPath addLineToPoint:CGPointMake(self.frame.size.width, 0.0)];

}

break;

case kLQQSideTypeLeft:

{

[aPath moveToPoint:CGPointMake(0.0, 0.0)];

[aPath addLineToPoint:CGPointMake(0.0, self.frame.size.height)];

}

break;

case kLQQSideTypeBottom:

{

[aPath moveToPoint:CGPointMake(0.0, self.frame.size.height)];

[aPath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];

}

break;

case kLQQSideTypeRight:

{

[aPath moveToPoint:CGPointMake(self.frame.size.width,0.0)];

[aPath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];

}

break;

default:

{

}

break;

}

layer.path = aPath.CGPath;

layer.strokeColor = color.CGColor;

layer.lineWidth = width;

[self.layer addSublayer:layer];

}

@end

以上便是此次分享的内容,期待大神多多指点补充,使其更加强壮!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值