iOS创建带删除线和价钱符号的Label
效果显示如下:
只需要子类化Label,重写DrawRect()方法即可:
- #import "MyLabel.h"
- @implementation MyLabel
- - (instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- if (self) {
- }
- return self;
- }
- //重写UILabel的drawRect类
- - (void)drawRect:(CGRect)rect{
- // 删除线
- if (_isDelete) {
- UIFont *font = [UIFont systemFontOfSize:self.font.pointSize];
- CGSize size = CGSizeMake(320, 2000);
- // 取得高度
- CGRect labelRect = [self.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];
- // 取得上下文
- CGContextRef c = UIGraphicsGetCurrentContext();
- // 设置删除线颜色
- CGContextSetStrokeColorWithColor(c, [UIColor redColor].CGColor);
- // 设置线宽
- CGContextSetLineWidth(c, 1);
- CGContextBeginPath(c);
- CGFloat halfWayUp = rect.size.height/2 + rect.origin.y;
- CGContextMoveToPoint(c, rect.origin.x , halfWayUp );//开始点
- CGContextAddLineToPoint(c, rect.origin.x + labelRect.size.width, halfWayUp);//结束点
- CGContextStrokePath(c);
- }
- if (_isRMB){
- UIFont *font = [UIFont systemFontOfSize:self.font.pointSize];
- UIFont *fuhaofont = [UIFont systemFontOfSize:20];
- UIColor *color = [UIColor redColor];
- CGSize size = CGSizeMake(320,2000);
- CGRect labelRect = [self.text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];
- NSString *fuhao = @"¥";
- CGContextRef c = UIGraphicsGetCurrentContext();
- CGContextSetStrokeColorWithColor(c, [UIColor redColor].CGColor);
- [fuhao drawAtPoint:CGPointMake(rect.size.width - labelRect.size.width - 100, 5) withAttributes:@{NSFontAttributeName:fuhaofont,NSForegroundColorAttributeName:color}];
- CGContextStrokePath(c);
- }
- // label的字体一直显示不出来,请注意一定要调用super
- [super drawRect:rect];
- }
iOS创建带删除线和价钱符号的Label的更多相关文章
- iOS_绘制带删除线的Label
效果图例如以下: 一个带删除线的文本标签,继承自UILabel 自绘代码过程例如以下: 1,重写控件的drawRect方法 2,首先得到上下文对象 3,设置颜色,并指定是填充(Fill)模式还是笔刷( ...
- iOS · UILabel加删除线
创建自定义子类DeleteLineLabel,继承自UILabel,然后在自定义子类DeleteLineLabel中 方法一(上下文): - (void)drawRect:(CGRect)rect { ...
- ios Lable 添加删除线
遇到坑了: NSString *goodsPrice = @"230.39"; NSString *marketPrice = @"299.99"; NSStr ...
- iOS创建、删除文件夹、获取沙盒路径
1.获取沙盒路径 // 获取沙盒路径 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent: ...
- python 对过时类或方法添加删除线的方法
class Cat(Animal): def __init__(self): import warnings warnings.warn("Cat类带删除线了", Deprecat ...
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...
- iOS 为label添加删除线
UILabel *testLabel = [[ UILabel alloc] initWithFrame:CGRectMake(, , , )]; testLabel.numberOfLines = ...
- ios 富文本 加颜色 删除线
UILabel *valueL = [JAppViewTools getLabel:CGRectMake(JFWidth(15), CGRectGetMaxY(proName.frame)+JFWid ...
- linux命令 - ln - 创建和删除软、硬链接
linux命令 - ln - 创建和删除软.硬链接 在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号.文件属性保存在索引结点里,在访问文件 ...
随机推荐
- Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)
Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...
- SSH环境搭建之Spring环境搭建篇
SSH环境搭建之Spring环境搭建篇 一.引入Spring所使用的JAR文件 二.在src目录下创建beans.xml(Spring的容器文件) <?xml version="1.0 ...
- C语言根据结构体成员变量的地址,得到结构体的地址
看nginx代码时发现双链表使用的是这种方法,记录一下 给出一个实例来说明 struct father_t { int a; char *b; double c;}f;char *p ...
- TODO redis学习笔记
redis官网教程地址:http://try.redis.io/ redis脚本和命令部分来自:https://www.runoob.com/redis/redis-security.html red ...
- Cloneable注解使用
使用 clone()方法的类必须 implement Cloneable 如果没有继承,clone()方法会报错 java.lang.CloneNotSupportedException异常
- mysql基础_操作文件中的内容
1.插入数据: insert into t1(id,name) values(1,'alex'); #向t1表中插入id为1,name为'alex'的一条数据 2.删除: delete from t1 ...
- CodeForces - 1202F You Are Given Some Letters... (整除分块)
题意:一个字符串包含a个A和b个B,求这个字符串所有可能的循环节长度(末尾可能存在不完整的循环节) 好题,但思路不是很好想. 首先由于循环节长度可以任意取,而循环次数最多只有$O(\sqrt n)$个 ...
- modbus_百度经验
转自:https://jingyan.baidu.com/article/2c8c281dbdfa9f0009252a74.html 图片都没了,百度真差劲---还是博客园好!!! ModBus通讯规 ...
- 菜鸟刷面试题(五、Java容器篇)
目录: java 容器都有哪些? Collection 和 Collections 有什么区别? List.Set.Map 之间的区别是什么? HashMap 和 Hashtable 有什么区别? 如 ...
- 终于结束的起点——Adios
准退役了一年,终于还是要结束了,整个高中的OI生涯就这么过去了. 今年的CSP-S又一次提醒我,我真的只是个蒟蒻,学了两年多还是这么个样.不过,这段OI生涯或许确实给我带来了许多. 一切的起点 17年 ...