效果显示如下:

只需要子类化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的更多相关文章

  1. iOS_绘制带删除线的Label

    效果图例如以下: 一个带删除线的文本标签,继承自UILabel 自绘代码过程例如以下: 1,重写控件的drawRect方法 2,首先得到上下文对象 3,设置颜色,并指定是填充(Fill)模式还是笔刷( ...

  2. iOS · UILabel加删除线

    创建自定义子类DeleteLineLabel,继承自UILabel,然后在自定义子类DeleteLineLabel中 方法一(上下文): - (void)drawRect:(CGRect)rect { ...

  3. ios Lable 添加删除线

    遇到坑了: NSString *goodsPrice = @"230.39"; NSString *marketPrice = @"299.99"; NSStr ...

  4. iOS创建、删除文件夹、获取沙盒路径

    1.获取沙盒路径 // 获取沙盒路径 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent: ...

  5. python 对过时类或方法添加删除线的方法

    class Cat(Animal): def __init__(self): import warnings warnings.warn("Cat类带删除线了", Deprecat ...

  6. iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

    iOS——文件操作NSFileManager (创建.删除,复制,粘贴)       iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...

  7. iOS 为label添加删除线

    UILabel *testLabel = [[ UILabel alloc] initWithFrame:CGRectMake(, , , )]; testLabel.numberOfLines = ...

  8. ios 富文本 加颜色 删除线

    UILabel *valueL = [JAppViewTools getLabel:CGRectMake(JFWidth(15), CGRectGetMaxY(proName.frame)+JFWid ...

  9. linux命令 - ln - 创建和删除软、硬链接

    linux命令 - ln - 创建和删除软.硬链接 在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号.文件属性保存在索引结点里,在访问文件 ...

随机推荐

  1. C - Calculation 2 HDU - 3501 (欧拉)

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  2. 实用:Java基础流计算

    java的流不常用,每次学习完都懂,过了一段时间就全忘了... 记录下一点实用的东西... 需求: 截取文件的前250kb内容 public static void main(String[] arg ...

  3. kotlin字符串比较&空值处理&when表达式

    字符串比较: 字符串的比较也是实际中比较常见的,下面来看下它的比较跟java中有啥不同,下面走起! 如我们所预期,其结果: 下面再来: 如果是java,结果肯定是为false,因为"==&q ...

  4. wiki页面文本挖掘

    import os,sysimport sysfrom bs4 import BeautifulSoupimport urllib.request# reload(sys)# sys.setdefau ...

  5. java与JSON

    XML 格式数据极其的冗长.因为每个离散的数据片段需要大量的 XML 结构,所有有效 的数据的比例非常低.XML 语法还有轻微的模糊.还有,解析 XML 是非常占程序员的精力的.你需要提前了解详细的结 ...

  6. P5025 [SNOI2017]炸弹 题解

    蒟蒻的第一篇黑题题解(学了这么长时间了才第一道也是没谁了.) 题目链接: Solution: 朴素: 根据题目描述,我们可以处理每一个x节点左右爆炸半径范围内的点,然后模拟一次爆炸 (for),遍历每 ...

  7. nginx限流&健康检查

    Nginx原生限流模块: ngx_http_limit_conn_module模块 根据前端请求域名或ip生成一个key,对于每个key对应的网络连接数进行限制. 配置如下: http模块   ser ...

  8. SpringBoot集成Druid实现监控

    application.properties文件完整信息 #连接数据库 spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spri ...

  9. asyncio模块实现线程的嵌套和穿插

    import asyncio import time now = lambda :time.time() async def cpc_1(x): print('正在烙比萨饼,预计{}分钟'.forma ...

  10. BZOJ1101——莫比乌斯函数&&入门

    题目 链接 有$50000$次查询,对于给定的整数$a,b$和$d$,有多少正整数对$x$和$y$,满足$x \leq a$,$y \leq b$,并且$gcd(x, y)=d$.$1 \leq k ...