利用CAShapeLayer在文字上画虚线(UILable举例)
来源:互联网 发布:解压rar文件的软件 mac 编辑:程序博客网 时间:2024/05/17 23:42
今天看到朋友有个需求,是要在lab的文字下画虚线,感觉很有意思就用CAShapeLayer研究了下,来一起看看吧。
老样子直奔主题上代码:
//// ViewController.m// DottedLineDemo//// Created by a111 on 16/3/16.// Copyright © 2016年 司小文. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; [self makeDottedLine]; // Do any additional setup after loading the view, typically from a nib.}#pragma mark 制作虚线- (void)makeDottedLine{ //lab NSString *str = @"司小文的博客:http://blog.csdn.net/siwen1990"; float strFont = 14.; CGRect labRect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:strFont]} context:nil]; UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-labRect.size.width)/2, 100, labRect.size.width,labRect.size.height)]; lab.textColor = [UIColor whiteColor]; lab.text = str; lab.font = [UIFont systemFontOfSize:strFont]; [self.view addSubview:lab]; //layer CAShapeLayer *shapeLayer = [CAShapeLayer layer]; [shapeLayer setBounds:lab.bounds]; [shapeLayer setPosition:lab.center]; [shapeLayer setFillColor:[[UIColor redColor] CGColor]]; //设置虚线的颜色 - 颜色请必须设置 [shapeLayer setStrokeColor:[[UIColor whiteColor] CGColor]]; //设置虚线的高度 [shapeLayer setLineWidth:1.0f]; //设置类型 [shapeLayer setLineJoin:kCALineJoinRound]; /* 10.f=每条虚线的长度 3.f=每两条线的之间的间距 */ [shapeLayer setLineDashPattern: [NSArray arrayWithObjects:[NSNumber numberWithInt:10.f], [NSNumber numberWithInt:3.f],nil]]; // Setup the path CGMutablePathRef path1 = CGPathCreateMutable(); /* 代表初始坐标的x,y x:写-2,是为了视觉上,虚线比文字稍长一点。 y:要和下面的y一样。 */ CGPathMoveToPoint(path1, NULL,-2, lab.frame.size.height); /* 代表坐标的x,y lab.frame.size.width+2:我觉得他代表的意思可以理解为线的长度。 lab.frame.size.height:要与上面的y大小一样,才能使平行的线,不然会画出斜线呦~ */ CGPathAddLineToPoint(path1, NULL, lab.frame.size.width+2,lab.frame.size.height); //赋值给shapeLayer [shapeLayer setPath:path1]; //清除 CGPathRelease(path1); //可以把self改成任何你想要的UIView. [[self.view layer] addSublayer:shapeLayer]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
效果图:
注释写的很清楚但是demo还是要奉上的:
CAShapeLayer画虚线-司小文 (提取码:e684)
感谢观看,学以致用更感谢。
0 0
- 利用CAShapeLayer在文字上画虚线(UILable举例)
- iOS 在UIView 上画虚线
- [iOS]UILable文字如何在顶部
- 利用shape画虚线
- 图片上画虚线
- 在UILable上添加点击事件
- 6.0 在picturebox上画虚线等不同类型的线
- 如何在屏幕上画虚线矩形框?
- 在图片上画文字
- Android利用shape画虚线
- android利用shape画虚线
- Android利用shape画虚线
- 利用opencv2函数putText()在图片上写文字
- UILable自适应文字高度
- 在UITableViewCell上建立UILable多行显示
- Java 利用Graphics2D在图片上画出竖排文字与横排文字
- 利用opencv将两张图像在同一窗口上显示,并在图片上显示文字
- 在任意位置画上文字
- 19.ELK实时日志分析平台之Elasticsearch REST API简介
- ios多图下载(多线程知识)
- Smarty选择和循环语句的使用
- 字节、KB、MB、GB 之间的换算关系 Bps和bps的区别
- ios中切圆形图的方法
- 利用CAShapeLayer在文字上画虚线(UILable举例)
- 【Hadoop 10周年】我与Hadoop不得不说的故事
- 65-题目1208:10进制 VS 2进制
- 简单介绍Java中Comparable和Comparator
- Catch him(BFS)
- Spring和SpringMVC的区别
- 网络最短路径Dijkstra算法
- 为了迅速求职Android岗位
- epoll、poll、select的比较