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

相关文章

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设置可以优化不同的网

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图