工具类(为控件设置圆角) - 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函数.在此写了一个通用的函数,只要在用到改变 ...
随机推荐
- centos7 安装jdk、Tomcat
1.安装jdk 下载jdk: 解压:tar -zxvf filename -C /usr/local/jdk8/ 配置环境变量: vim /etc/profile 添加如下内容:JAVA_HOME根据 ...
- RestTemplate请求出现401错误
最近遇到一个请求API接口总是报401 Unauthorized错误,起初是认为这个是平台返回的,后来用Postman请求,发现平台其实返回的是一串json,里面带有一些权限验证失败的消息,但到我们代 ...
- MySQL8.0加载文件内容报错: ERROR 1148: The used command is not allowed with this MySQL version
mysql数据库将文件内容加载到表中报错: mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet LINES TERMINAT ...
- 一个基于Ionic3.x cordova的移动APP demo
项目地址如遇网络不佳,请移步国内镜像加速节点 前端技术: Angular4.x + ionic3.x + cordova 项目运行: git clone git@github.com:EasyTuan ...
- asp: suggest ajax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQLAlchemy的使用---M2M增删改查
from sqlalchemy.orm import sessionmaker from sqlalchemy_M2M import engine, Girls, Boys Session = ses ...
- maven项目的创建
·做了两年多Java Web一多半的项目都是SSM架构的,只搭建过两次,趁着周末做个总结整理. Eclipse搭建Maven项目 1.new project --> Maven project ...
- 如何在web.config文件中配置Session变量的生命周期
实例说明:在网上购物商城中,为了维护在线购物环境,一般只有注册会员才可以购买商品.实现购物功能时,先通过Session变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...
- C++ 输出菱形
输出*号组成的菱形: // print.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...
- dispatchTouchEvent,onTouchEvent与onInterceptTouchEvent
1.首先明白一个常识:View 没有onInterceptTouchEvent事件,而ViewGroup这三个事件都有,是viewgroup继承View之后才加了一个方法叫onIntercepTouc ...