为了便于日常开发效率,因此创建了一些小的工具类便于使用.
具体 code 如下:
声明:

/*
为控件添加边框样式_工具类
*/
#import <UIKit/UIKit.h> 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

 

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

工具类(为控件设置圆角) - iOS的更多相关文章

  1. 工具类(为控件设置色值) - iOS

    为了便于日常开发效率,因此创建了一些小的工具类便于使用.具体 code 如下:声明: /* 为控件设置色值 */ #import <UIKit/UIKit.h> @interface UI ...

  2. iOS之用xib给控件设置圆角、边框效果

    xib中为各种控件设置圆角 通过代码的方式设置 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *my ...

  3. 我的QT5学习之路(三)——模板库、工具类和控件(下)

    一.前言 作为第三篇的最后一部分,我们来看一下Qt的控件,谈到控件,就会让人想到界面的美观性和易操作性,进而想到开发的便捷性.作为windows界面开发的MFC曾经是盛行了多少年,但是其弊端也随着其他 ...

  4. iOS之分别使用代码和storyboard、xib为控件设置圆角(以按钮为例)

    首先我们看一下代码是如何给按钮设置圆角的: 我们再来看看如何在storyboard或xib中给按钮设置圆角: 1.在storyboard或xib中添加按钮后,设置标题和背景色,做好约束: 2.点击 S ...

  5. Chapter2:Qt5模板库,工具类及控件

    2.1 字符串类 QString类保存16位Unicode值,提供了丰富的操作,查询和转换等函数.  (1):QString提供了一个二元的"+"操作符用于组合两个字符串  (2) ...

  6. iOS 在xib或storyboard里为控件添加圆角、外框和外框颜色

    如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以 layer.borderWidth     设置外框宽度属性 layer.cornerRadius    设置圆角属性 ...

  7. iOS在xib或storyboard里为控件添加圆角、外框和外框颜色

    如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以: layer.borderWidth 设置外框宽度属性 layer.cornerRadius 设置圆角属性 只要为属性 ...

  8. Xib中设置控件的圆角、边框效果

    设置控件的圆角和边框效果有两种方式: 1.代码实现: self.myView.layer.masksToBounds = YES; self.myView.layer.cornerRadius = ; ...

  9. 将控件画成圆角的效果(Delphi)

    最近在做一个Delphi的项目,常常要设计软件的界面,需要将控件画成圆角矩形.在Delphi中将控件画成圆角效果,可使用CreateRoundRectRgn函数.在此写了一个通用的函数,只要在用到改变 ...

随机推荐

  1. Windows无法启动MySQL服务,错误1067

    问题:mysql服务启动异常 找出原因 检查D:\mysql-5.6.20-winx64\data目录下client-02.err文件的错误信息(以err为后缀名的文件是mysql的日志文件) 修改配 ...

  2. C# 判断List集合中是否有重复的项

    /*在.Net 3.5 以上*/ ).Count() >= ;

  3. Spring学习笔记:Spring概述,第一个IoC依赖注入案例

    一.Spring的优点 企业及系统: 1.大规模:用户数量多.数据规模大.功能众多 2.性能和安全要求高 3.业务复杂 4.灵活应变 Java技术:高入侵式依赖EJB技术框架-->Spring框 ...

  4. 一般处理程序、ASP.NET和MVC的区别

    这个问题说起来,我有点惭愧 想当初在大学里学的就是ASP.NET WebForms 在实习期间也是用的WebForms来开发网站,然后就觉得.NET开发网站就是用这个开发模式 现在想想都想笑...实在 ...

  5. Python中and和or的运算法则

    1. 在纯and语句中,如果每一个表达式都不是假的话,那么返回最后一个,因为需要一直匹配直到最后一个.如果有一个是假,那么返回假2. 在纯or语句中,只要有一个表达式不是假的话,那么就返回这个表达式的 ...

  6. 基于CSS3的3D旋转效果

    自从有了html5和css3,好多以前只能想想的华丽效果都可以上手实现了.3D 转换(个人认为3D变换更贴切^)就是其中之一.关于3D转换,可以阅读CSS3 3D transform变换,不过如此,文 ...

  7. IE浏览器下的渐变背景

    background: linear-gradient(to bottom, #000000 0%,#ffffff 100%);(标准) linear-gradient 在 ie9 以下是不支持的,所 ...

  8. Android XMPP 例子(Openfire+asmack+spark) 出现登陆连接错误

    Android XMPP 例子(Openfire+asmack+spark) 运行出来没问题,但是登陆的时候出现如下错误: 出现错误: 09-17 15:24:16.388: E/AndroidRun ...

  9. 【Linux】 Linux编程规范&Linux 编程环境搭建

    一.通过Samba映射网络驱动器 菜单栏-计算机-映射网络驱动器 English 菜单栏-Home -Easy access-Map as drive 编辑代码使用 Windows 编译 运行程序在 ...

  10. 使用gulp解决外部编辑器修改Eclipse文件延迟刷新

    本人前端用惯了Hbuilder,修改了eclipse项目中的文件后,由于是外部编辑器修改过的,eclipse不会自动部署更新,一般按F5刷新项目,或者在 preferences > genera ...