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号.文件属性保存在索引结点里,在访问文件 ...
随机推荐
- LoadRunner(7)
一.参数化策略 1.Select next row(How? 如何取?)取值方式 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下取值: 数据取完可以从头循环重复使用 ...
- java线程基础巩固---如何实现一个自己的显式锁Lock
拋出synchronized问题: 对于一个方法上了同锁如果被一个线程占有了,而假如该线程长时间工作,那其它线程不就只能傻傻的等着,而且是无限的等这线程工作完成了才能执行自己的任务,这里来演示一下这种 ...
- ArcGIS水文分析实战教程(15)库容和淹没区计算
库容和淹没区计算 的基本流程 要计算库容就必须先计算出该集水区面积,并且通过不同的水位计算出淹没区,并利用淹没区去裁剪DEM数据,将水面与下垫面的体积计算出来,这就是水库的库容.由于有了前面的基础,这 ...
- BIgDecimal输出时添加金额分割符
1.创建Serializer文件 2.修改输出方法 3.使用
- linux基础_vi和vim快捷键
(1)拷贝当前行 yy,拷贝当前行向下5行 5yy,并粘贴. (2)删除当前行 dd, 删除当前行向下的5行 5dd. (3)在文件中查找某个单词.[在命令行下使用 /+关键字,回车查找,输入n就是查 ...
- [转]web.xml中servlet ,filter ,listener ,interceptor的作用与区别
原文链接:https://blog.csdn.net/netdevgirl/article/details/51483273 一.概念: 1.servlet:servlet是一种运行服务器端的java ...
- Ubuntu14版桌面突然卡住怎么办
参考:https://blog.csdn.net/hautxuhaihu/article/details/78924926 (1)ctrl+alt+f1...6进入命令行终端.用户名,密码登录. (2 ...
- Markdown使用教程(转载收藏)
基础语法 标题 Markdown支持6种级别的标题,对应html标签 h1 ~ h6 # h1 ## h2 ### h3 #### h4 ##### h5 ###### h6 以上标记效果如下: h1 ...
- Bootstrap Popover(弹出框)弹出自定义格式代码
HEAD 标签之间引入CSS:<link href="../../../public/css/bootstrap.min.css" rel="stylesheet& ...
- fatal: refusing to merge unrelated histories(git pull)
https://blog.csdn.net/lindexi_gd/article/details/52554159 (refusing to merge unrelated histories) ht ...