为了便于日常开发效率,因此创建了一些小的工具类便于使用.
具体 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. 上传文件插件-bootstrap-fileinput

    1. js文件: <link href="/bootstrap/css/fileinput.css" media="all" rel="styl ...

  2. vs2013项目停止调试后 iis express也跟着退出

    解决方法:项目—>XX属性—>Web—>调试器—>取消[启用编辑并继续]

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

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

  4. CSS的两种格式化上下文:BFC和IFC

    CSS的两种格式化上下文   文章包含很多个人理解,如果错误,欢迎指出~   在看本文之前,你要对CSS的盒子模型,Block-Level元素,Inline-Level元素有所了解,具体可参考CSS的 ...

  5. Drupal Module Hooks

    Drupal is a Content Management System. Drupal is also deeply, deeply weird. While systems like Magen ...

  6. Network Embedding 论文小览

    Network Embedding 论文小览 转自:http://blog.csdn.net/Dark_Scope/article/details/74279582,感谢分享! 自从word2vec横 ...

  7. 2016微软技术大会Azure相关回顾

    3 天的时间稍纵即逝,伴随着本届大会压轴大奖的揭晓,2016 年度的微软技术大会完美落幕.以“数字化转型”为主题,来自微软全球的近百位顶尖技术专家.工程师和业务负责人拔冗而至,在 130 余场的专业技 ...

  8. css加载是否会阻塞dom树渲染

    这里说的是头部引入css的情况 首先,我们都知道:css是由单独的下载线程异步下载的. 咱们先分析下css加载会影响什么,刚才的问题太笼统了,咱们需要细化一下. 会影响什么呢? 一个就是DOM树解析, ...

  9. 记录项目代码迁移后,UI测试框架的搭建(配置文件的修改、测试脚本试运行)

    前文:记录一次项目代码迁移过程 上文代码迁移的目的就是为了新增vue脚手架自带的UI测试框架,工具有了,就需要实践运行在项目中了(修改配置文件.编写测试脚本等). 一.单元测试 测试框架 karma ...

  10. WAKE-SPM-综述

    1,SPM 1,1source paper:http://lear.inrialpes.fr/pubs/2007/ZMLS07/ZhangMarszalekLazebnikSchmid-IJCV07- ...