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

相关文章

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永不超时的方法(一)

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

mtu设置多少网速最快? 路由器MTU设置最佳网速的技巧

《mtu设置多少网速最快?路由器MTU设置最佳网速的技巧》mtu设置多少网速最快?想要通过设置路由器mtu获得最佳网速,该怎么设置呢?下面我们就来看看路由器MTU设置最佳网速的技巧... 答:1500 MTU值指的是在网络传输中数据包的最大值,合理的设置MTU 值可以让网络更快!mtu设置可以优化不同的网