iOS控件设置虚线框

2023-11-23 14:38
文章标签 设置 ios 控件 线框

本文主要是介绍iOS控件设置虚线框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

通过CAShapeLayer和UIBezierPath给控件添加虚线或设置虚线条。备用

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self createShapeLayer_A];

   [self createShapeLayerLine];

    [self createShapeLayer_B];

 

}

//侧边的虚线:左和右。

- (void)createShapeLayer_A{

    UILabel *typeLabel = [self createLabelFrame:CGRectMake(100, 200,100, 80) TextContent: @"ShapeLayer_A" Font:[UIFont systemFontOfSize:15] BackgroundColor:[UIColor cyanColor] TextAlignment:NSTextAlignmentCenter];

    [self.view addSubview:typeLabel];

    CAShapeLayer *shapeLayerLeft = [self hs_ShapeLayerMakerWithStrokeColor:[UIColor cyanColor]

                                                             FillColor:[UIColor whiteColor]

                                                             BezierPathWithRect:CGRectMake(0, 0, 3, 80)

                                                             LineWidth:3.0

                                                             LineDashPattern:@[@1,@3]

                                                             BorderFrame:typeLabel.bounds];

    [typeLabel.layer addSublayer:shapeLayerLeft];

    CAShapeLayer *shapeLayerRight = [self hs_ShapeLayerMakerWithStrokeColor:[UIColor cyanColor]

                                                             FillColor:[UIColor whiteColor]

                                                    BezierPathWithRect:CGRectMake(97, 0,3, 80)

                                                             LineWidth:3.0

                                                       LineDashPattern:@[@1,@3]

                                                           BorderFrame:typeLabel.bounds];

    [typeLabel.layer addSublayer:shapeLayerRight];

    typeLabel.layer.masksToBounds = YES;//修剪一下

}

 

//设置虚线条

- (void)createShapeLayerLine{

    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 290, [UIScreen mainScreen].bounds.size.width, 1)];

    lineView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:lineView];

    CAShapeLayer *shape = [self hs_ShapeLayerMakerWithStrokeColor:[UIColor lightGrayColor] FillColor:[UIColor whiteColor] BezierPathWithRect:CGRectMake(0, 0, lineView.frame.size.width, 1) LineWidth:2 LineDashPattern:@[@2,@3] BorderFrame:lineView.bounds];

    [lineView.layer addSublayer:shape];

    lineView.layer.masksToBounds = YES;

}

/**

返回CAShapeLayer对象

 

@param strokeColor 虚线的颜色

@param fillColor 虚线间填充的颜色

@param bezierRect 设置虚线路径,决定了虚线存在于哪个位置

@param lineWidth 虚线宽度

@param lineDashPatter @[@a,@b] a:虚线高度,b:两虚线间隔高度

@param frame shapeLayer对象的frame

@return 返回CAShapeLayer对象从而添加到指定控件

*/

- (CAShapeLayer *)hs_ShapeLayerMakerWithStrokeColor:(UIColor *)strokeColor FillColor:(UIColor *)fillColor BezierPathWithRect:(CGRect)bezierRect LineWidth:(CGFloat)lineWidth LineDashPattern:(NSArray*)lineDashPatter BorderFrame:(CGRect)frame{

    CAShapeLayer *border = [CAShapeLayer layer];

    border.strokeColor = strokeColor.CGColor;

    border.fillColor = fillColor.CGColor;

    border.path = [UIBezierPath bezierPathWithRect:bezierRect].CGPath;

    border.lineWidth = lineWidth;//虚线伸出去的宽度

    border.lineDashPattern = lineDashPatter;//虚线高度 & 两虚线间隔高度

    border.frame = frame;

    return border;

}

 

//控件整个虚线框

- (void)createShapeLayer_B{

    UILabel *typeLabel = [self createLabelFrame:CGRectMake(100, 300,100, 80) TextContent: @"ShapeLayer_B" Font:[UIFont systemFontOfSize:15] BackgroundColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentCenter];

    [self.view addSubview:typeLabel];

    

    CAShapeLayer *shapeLayer = [self hs_AllRoundedShapeLayerWithStrokeColor:[UIColor cyanColor]

                                                             FillColor:nil

                                             BezierPathWithRoundedRect:typeLabel.bounds

                                                           CornerRadius:5

                                                             LineWidth:1.0

                                                       LineDashPattern:@[@4,@2]

                                                           BorderFrame:typeLabel.bounds];

    

    typeLabel.layer.cornerRadius = 5;

    typeLabel.layer.masksToBounds = YES;//修饰一下

    [typeLabel.layer addSublayer:shapeLayer];

}

 

 

/**

控件四周都是虚线

@param strokeColor 虚线的颜色

@param fillColor 虚线间填充的颜色

@param bezierRect 设置虚线路径

@param cornerRadius 圆角大小

@param lineWidth 虚线伸出去的宽度

@param lineDashPatter @[@a,@b] a:虚线高度,b:两虚线间隔高度

@param frame shapeLayer对象的frame

@return 返回CAShapeLayer对象从而添加到指定控件

*/

- (CAShapeLayer *)hs_AllRoundedShapeLayerWithStrokeColor:(UIColor *)strokeColor FillColor:(UIColor *)fillColor BezierPathWithRoundedRect:(CGRect)bezierRect CornerRadius:(CGFloat)cornerRadius LineWidth:(CGFloat)lineWidth LineDashPattern:(NSArray*)lineDashPatter BorderFrame:(CGRect)frame{

    CAShapeLayer *border = [CAShapeLayer layer];

    border.strokeColor = strokeColor.CGColor;

    border.fillColor = fillColor.CGColor;

    border.path = [UIBezierPath bezierPathWithRoundedRect:bezierRect cornerRadius:cornerRadius].CGPath;

    border.lineWidth = lineWidth;

    border.lineDashPattern = lineDashPatter;

    border.frame = frame;

    return border;

}

 

//创建Label对象

- (UILabel *)createLabelFrame:(CGRect)labelFrame TextContent:(NSString *)text Font:(UIFont*)font BackgroundColor:(UIColor *)backgroundColor TextAlignment:(NSTextAlignment)textAlignment{

    UILabel*typeLabel = [[UILabel alloc]initWithFrame:labelFrame];

    typeLabel.text = text;

    typeLabel.numberOfLines = 0;

    typeLabel.font = font;

    typeLabel.backgroundColor = backgroundColor;

    typeLabel.textAlignment = textAlignment;

    return typeLabel;

}

这篇关于iOS控件设置虚线框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/418614

相关文章

Java实现为PDF设置背景色和背景图片

《Java实现为PDF设置背景色和背景图片》在日常的文档处理中,PDF格式因其稳定性和跨平台兼容性而广受欢迎,本文将深入探讨如何利用Spire.PDFforJava库,以简洁高效的方式为你的PDF文档... 目录库介绍与安装步骤Java 给 PDF 设置背景颜色Java 给 PDF 设置背景图片总结在日常的

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

python库pydantic数据验证和设置管理库的用途

《python库pydantic数据验证和设置管理库的用途》pydantic是一个用于数据验证和设置管理的Python库,它主要利用Python类型注解来定义数据模型的结构和验证规则,本文给大家介绍p... 目录主要特点和用途:Field数值验证参数总结pydantic 是一个让你能够 confidentl

Java利用Spire.XLS for Java设置Excel表格边框

《Java利用Spire.XLSforJava设置Excel表格边框》在日常的业务报表和数据处理中,Excel表格的美观性和可读性至关重要,本文将深入探讨如何利用Spire.XLSforJava库... 目录Spire.XLS for Java 简介与安装Maven 依赖配置手动安装 JAR 包核心API介

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

小白也能轻松上手! 路由器设置优化指南

《小白也能轻松上手!路由器设置优化指南》在日常生活中,我们常常会遇到WiFi网速慢的问题,这主要受到三个方面的影响,首要原因是WiFi产品的配置优化不合理,其次是硬件性能的不足,以及宽带线路本身的质... 在数字化时代,网络已成为生活必需品,追剧、游戏、办公、学习都离不开稳定高速的网络。但很多人面对新路由器

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)