工具类(为控件设置圆角) - iOS
为了便于日常开发效率,因此创建了一些小的工具类便于使用.
具体 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的更多相关文章
- 工具类(为控件设置色值) - iOS
为了便于日常开发效率,因此创建了一些小的工具类便于使用.具体 code 如下:声明: /* 为控件设置色值 */ #import <UIKit/UIKit.h> @interface UI ...
- iOS之用xib给控件设置圆角、边框效果
xib中为各种控件设置圆角 通过代码的方式设置 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *my ...
- 我的QT5学习之路(三)——模板库、工具类和控件(下)
一.前言 作为第三篇的最后一部分,我们来看一下Qt的控件,谈到控件,就会让人想到界面的美观性和易操作性,进而想到开发的便捷性.作为windows界面开发的MFC曾经是盛行了多少年,但是其弊端也随着其他 ...
- iOS之分别使用代码和storyboard、xib为控件设置圆角(以按钮为例)
首先我们看一下代码是如何给按钮设置圆角的: 我们再来看看如何在storyboard或xib中给按钮设置圆角: 1.在storyboard或xib中添加按钮后,设置标题和背景色,做好约束: 2.点击 S ...
- Chapter2:Qt5模板库,工具类及控件
2.1 字符串类 QString类保存16位Unicode值,提供了丰富的操作,查询和转换等函数. (1):QString提供了一个二元的"+"操作符用于组合两个字符串 (2) ...
- iOS 在xib或storyboard里为控件添加圆角、外框和外框颜色
如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以 layer.borderWidth 设置外框宽度属性 layer.cornerRadius 设置圆角属性 ...
- iOS在xib或storyboard里为控件添加圆角、外框和外框颜色
如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以: layer.borderWidth 设置外框宽度属性 layer.cornerRadius 设置圆角属性 只要为属性 ...
- Xib中设置控件的圆角、边框效果
设置控件的圆角和边框效果有两种方式: 1.代码实现: self.myView.layer.masksToBounds = YES; self.myView.layer.cornerRadius = ; ...
- 将控件画成圆角的效果(Delphi)
最近在做一个Delphi的项目,常常要设计软件的界面,需要将控件画成圆角矩形.在Delphi中将控件画成圆角效果,可使用CreateRoundRectRgn函数.在此写了一个通用的函数,只要在用到改变 ...
随机推荐
- java字节码速查笔记
java字节码速查笔记 发表于 2018-01-27 | 阅读次数: 0 | 字数统计: | 阅读时长 ≍ 执行原理 java文件到通过编译器编译成java字节码文件(也就是.class文件) ...
- 中值滤波C语言优化
中值滤波C语言优化 图像平滑是图像预处理的基本操作,本文首先用不同的方法对一张图片做预处理比较它们效果的不同,然后针对中值滤波,实现了一种快速实现.(其实是copy的opencv实现,呵呵).因为op ...
- Flowchart
1. 工具可使用 https://www.processon.com 2.
- Dubbo架构原理
1 Dubbo核心功能 Remoting:远程通讯,提供对多种NIO框架抽象封装,包括“同步转异步”和“请求-响应”模式的信息交换方式. Cluster: 服务框架,提供基于接口方法的透明远程过程调用 ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 1、安装前准备
安装前准备 上传文件到服务器,x-ftp xshell登陆Centos 检查机器名 修改机器名为:portal.cloud.local 方法一:零时设置,重启后失效,该方法不可取 方法 ...
- 树莓派3(Raspberry pi 3)刷OpenWrt
原文在 https://my.oschina.net/wangandi/blog/687389 1.下载镜像,这个lede好像是openwrt的一个分支,openwrt本身还没有支持pi3,https ...
- Linux学习笔记之Linux第一课-基本介绍
Linux简介 Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux是一套免费使用和自由传播的类Unix操作系统,是一个基 ...
- 通过调用Word模板(Doc、dot)直接打印 z
通过替换模板中的指定 书签 来进行内容的替换.整合,然后直接发送到打印打印,也可以导出.即把打印出的语句换成保存函数. public static class myPrintByOffice ...
- c++链表实现学生成绩管理系统(简易版)
#include<iostream> using namespace std; typedef struct student{ int id;//学号 string sex; string ...