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号.文件属性保存在索引结点里,在访问文件 ...
随机推荐
- mysqll中索引详细讲解
MySQL(五) MySQL中的索引详讲 序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于My ...
- gdb 使用finish命令(缩写fin)
gdb 使用finish命令(缩写fin) gdb 使用finish命令(缩写fin) gdb 使用finish命令(缩写fin) 跳出跟踪的函数
- vue-cli3.0 环境变量与模式
vue-cli3.0移除了配置文件目录: config和build文件夹.可以说是非常的精简了,那移除了配置文件目录后如何自定义配置环境变量和模式呢? 为什么需要配置环境变量和模式呢? 所有方法肯定是 ...
- c#系统泛型委托
Action<T> 无返回值的系统泛型委托 namespace ConsoleApp1 { public class UserInfo { public int Id { get; set ...
- c语言第一次作业1
第一次作业 一 你对软件工程或者计算机科学与技术专业的了解是什么? 软件工程是一门研究用工程化方法构建和维护有效的,实用的和高质量的软件的学科,涉及程序语言设计,数据库,软件开发工具,系统平台,设计模 ...
- UnhandledExceptionPolicy
winform程序未捕获异常解决方法 EventType clr20r3 P1 http://blog.csdn.net/chichaodechao/article/details/8294922
- demo(一) react-native-router-flux
react-native init AwesomeProject cd AwesomeProject 安装模块 npm i react-native-router-flux --save
- 如何复制word的图文到ueditor中自动上传?
官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...
- HGOI 20191107 题解
Problem A 树状数组 给出下列$C++$代码: 设区间加操作$modify(l,r)$为调用两次$update(r,1)$和$update(l-1,-1)$ 设$f(l,r)$表示在初始$cn ...
- jQuery系列(十四):jQuery中的ajax
1.什么是ajax AJAX = 异步的javascript和XML(Asynchronous Javascript and XML) 简言之,在不重载整个网页的情况下,AJAX通过后台加载数据,并在 ...