ios Lable 添加删除线
遇到坑了:
NSString *goodsPrice = @"230.39";
NSString *marketPrice = @"299.99";
NSString* prceString = [NSString stringWithFormat:@"%@ %@",goodsPrice,marketPrice];
DLog(@"----打印--%@---",prceString); NSMutableAttributedString*attributedString = [[NSMutableAttributedString alloc]initWithString:prceString]; [attributedString addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(, , , ) range:NSMakeRange(, goodsPrice.length)]; [attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:[prceString rangeOfString:marketPrice]]; [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:] range:[prceString rangeOfString:marketPrice]]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:[prceString rangeOfString:marketPrice]];
[priceLabel setAttributedText:attributedString];
我感觉下面的代码写的没有问题,但是运行起来怎么就不行了呢

真是百思不得姐,然后各种百度:http://stackoverflow.com/questions/43070335/nsstrikethroughstyleattributename-how-to-strike-out-the-string-in-ios-10-3

然后然后...,换了种写法:
NSString *goodsPrice = @"230.39";
NSString *marketPrice = @"299.99";
NSString* prceString = [NSString stringWithFormat:@"%@ %@",goodsPrice,marketPrice];
DLog(@"----打印--%@---",prceString); NSMutableAttributedString *attritu = [[NSMutableAttributedString alloc]initWithString:prceString];
[attritu addAttributes:@{
NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),
NSForegroundColorAttributeName:
[UIColor lightGrayColor],
NSBaselineOffsetAttributeName:
@(),
NSFontAttributeName: [UIFont systemFontOfSize:]
} range:[prceString rangeOfString:marketPrice]];
priceLabel.attributedText = attritu;

如果上面的问题还有问题,我们还可以用其他方式实现这种效果
新建一个集成 UILabel 的子类,
- (void)drawRect:(CGRect)rect
{
// 调用super的drawRect:方法,会按照父类绘制label的文字
[super drawRect:rect]; // 取文字的颜色作为删除线的颜色
[self.textColor set];
CGFloat w = rect.size.width;
CGFloat h = rect.size.height;
// 绘制(这个数字是为了找到label的中间位置,0.35这个数字是试出来的,如果不在中间可以自己调整)
UIRectFill(CGRectMake(, h * 0.5, w, ));
}
别忘了sizeToFit 不然线会根据空间的 width来画的
YJlale *priceLabel = [[YJlale alloc] initWithFrame:CGRectMake(, , , )];
priceLabel.textColor = [UIColor redColor];
priceLabel.text = @"";
[priceLabel sizeToFit];
[self.view addSubview:priceLabel];

ios Lable 添加删除线的更多相关文章
- iOS label 添加删除线(删划线)遇到的坑
1.添加删划线方法遇到的问题 -(void)lastLabelDeal:(NSString *)str1 string:(NSString *)str2 label:(UILabel *)label{ ...
- iOS 为label添加删除线
UILabel *testLabel = [[ UILabel alloc] initWithFrame:CGRectMake(, , , )]; testLabel.numberOfLines = ...
- iOS · UILabel加删除线
创建自定义子类DeleteLineLabel,继承自UILabel,然后在自定义子类DeleteLineLabel中 方法一(上下文): - (void)drawRect:(CGRect)rect { ...
- Android中TextView添加删除线
项目中的需求~~~~ 商城中物品的一个本身价格,还有一个就是优惠价格...需要用到一个删除线. public class TestActivity extends Activity { private ...
- python 对过时类或方法添加删除线的方法
class Cat(Animal): def __init__(self): import warnings warnings.warn("Cat类带删除线了", Deprecat ...
- iOS创建带删除线和价钱符号的Label
效果显示如下: 只需要子类化Label,重写DrawRect()方法即可: #import "MyLabel.h" @implementation MyLabel - (insta ...
- Android: 在 TextView 里使用删除线
Android: 在 TextView 里使用删除线 分类: Android2014-09-25 13:17 3431人阅读 评论(0) 收藏 举报 以编程的方式添给 TextView 添加删除线: ...
- iOS仿网易新闻栏目拖动重排添加删除效果
仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...
- iOS-属性字符串添加下划线、删除线
常用到的属性字符串 ///定义属性字符串NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithStrin ...
随机推荐
- python 基础 字典生成式
dict1 = {1:2,3:4,6:7,9:10} print dict((v,k) for k,v in dict.items()) 结果 {2:1.4:3,10:9,7:6} res = [{' ...
- Sequence Models 笔记(一)
1 Recurrent Neural Networks(循环神经网络) 1.1 序列数据 输入或输出其中一个或两个是序列构成.例如语音识别,自然语言处理,音乐生成,感觉分类,dna序列,机器翻译,视频 ...
- Intent的简单概述
Intent是负责在系统组件之间传递信息的一个对象,就像名字一样,是一个意图,可以将当前组件的意图传递给系统,例如启动Activity等,还可以在传递的时候附加上一些值,可以用Bundle对象封装这些 ...
- C++经典题目:约瑟夫环问题
问题描述: 有n个人围成一圈,顺序排号.从第一个人开始报数(1~3报数),凡报到3的人退出圈子,问最后留下的人原来排在第几号. 分析: 首先由用户输入人数n,然后对这n个人进行编号[因为如果不编号的话 ...
- [原创]SQL表值函数:返回从当前周开始往回的自定义周数
一如往常一样,一篇简短博文记录开发过程中遇到的一个问题.初衷都是记录自己的一些Idea,也是希望能够帮助一些凑巧遇到此类需求的问题,这个需求的的开端是因为,要统计最近N周的销售数据. 接下来我们来看看 ...
- 12、geo数据上传
1.注册一个NCBI账户 注册geo账户(老用户和新用户): https://www.ncbi.nlm.nih.gov/geo/submitter/ 有3个月的时间 GEO DataSets > ...
- sublime text 3如何安装插件和设置字号
使用ctrl + ~(这个符号是键盘上1前面那个),如果不能调用出就需要修改快捷键,在Preferences ->Key Bindings - Default打开文件后,大概在248行,这里我修 ...
- idea中,使用facets添加完web后,项目已变为web项目,但web.xml中内容经常变为红色,并报错,如何解决?
这中错误经常是由于配置facets并添加完web后,没有进一步配置web.xml文件,导致web.xml是使用系统默认的. 如图:需要进一步配置web.xml文件,使用我们src/main/webap ...
- jexus处理静态文件(处理后缀)
AspNet_Exts=txt就能把你指定的扩展名交给asp.net处理.同理,可以写很多个,AspNet_Exts=txt,htm,html
- IOHelper(自制常用的输入输出的帮助类)
常用的读写文件,和地址转换(win和linux均支持),操作文件再也不是拼接那么的low了 using System; using System.Diagnostics; using System.I ...