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

相关文章

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

PyCharm如何设置新建文件默认为LF换行符

《PyCharm如何设置新建文件默认为LF换行符》:本文主要介绍PyCharm如何设置新建文件默认为LF换行符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm设置新建文件默认为LF换行符设置换行符修改换行符总结PyCharm设置新建文件默认为LF

Linux上设置Ollama服务配置(常用环境变量)

《Linux上设置Ollama服务配置(常用环境变量)》本文主要介绍了Linux上设置Ollama服务配置(常用环境变量),Ollama提供了多种环境变量供配置,如调试模式、模型目录等,下面就来介绍一... 目录在 linux 上设置环境变量配置 OllamPOgxSRJfa手动安装安装特定版本查看日志在

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4

如何关闭 Mac 触发角功能或设置修饰键? mac电脑防止误触设置技巧

《如何关闭Mac触发角功能或设置修饰键?mac电脑防止误触设置技巧》从Windows换到iOS大半年来,触发角是我觉得值得吹爆的MacBook效率神器,成为一大说服理由,下面我们就来看看mac电... MAC 的「触发角」功能虽然提高了效率,但过于灵敏也让不少用户感到头疼。特别是在关键时刻,一不小心就可能触

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题