iOS Label添加删除线

@import url(/css/cuteeditor.css);

在做优惠价格的时候需要用到删除线,但是网上的删除线都是创建一个类继承自UILabel,然后重写drawRect方法重绘Label,其实苹果的NSAttributedString就可以实现这一点。

代码如下:

NSString *lastPrice = @"¥12.25";

NSUInteger length = [lastPrice length];

NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:lastPrice];

[attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];

[attri addAttribute:NSStrikethroughColorAttributeName value:(id)[UIColor blackColor] range:NSMakeRange(0, length)];

[lastPriceLabel setAttributedText:attri];

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

时间: 2024-08-02 20:35:58

iOS Label添加删除线的相关文章

iOS label 添加删除线(删划线)遇到的坑

1.添加删划线方法遇到的问题 -(void)lastLabelDeal:(NSString *)str1 string:(NSString *)str2 label:(UILabel *)label{ NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@  %@",str1,str2]]; [str addAtt

【iOS知识学习】_iOS Label添加删除线

在做优惠价格的时候需要用到删除线,但是网上的删除线千篇一律,都是大抄小抄,其实苹果的NSAttributedString就可以实现这一点啦. 代码如下: NSString *oldPrice = @"¥ 12345"; NSUInteger length = [oldPrice length]; NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];

iOS 为label添加删除线

UILabel *testLabel = [[ UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 100)]; testLabel.numberOfLines = 0; NSString* strText = @"测试画删除线测试画删除线测试画删除线测试画删除线测试画删除线"; NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initW

给UILabel添加删除线

给UILabel添加删除线 by 伍雪颖 let attributedText = NSAttributedString(string: "道之不存,师之所存也", attributes: [NSStrikethroughStyleAttributeName:1]) self.contentLabel.attributedText = attributedText

iOS · UILabel加删除线

创建自定义子类DeleteLineLabel,继承自UILabel,然后在自定义子类DeleteLineLabel中 方法一(上下文): 1 - (void)drawRect:(CGRect)rect { 2 [super drawRect:rect]; 3 4 CGContextRef ref = UIGraphicsGetCurrentContext(); 5 6 //绘制起点 7 CGContextMoveToPoint(ref, 0, rect.size.height * 0.5); 8

Android中TextView添加删除线

项目中的需求~~~~ 商城中物品的一个本身价格,还有一个就是优惠价格...需要用到一个删除线. public class TestActivity extends Activity { private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

iOS 为label加删除线

NSString *oldPrice = [NSString stringWithFormat:@"原价 %@",_item.previousPrice.stringValue]; NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:oldPrice]; [attr addAttribute:NSStrikethroughStyleAttributeName value:@

[代码]label增加删除线

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 100, 30)];    [self.view addSubview:lable];    lable.backgroundColor = [UIColor redColor];    lable.textAlignment = NSTextAlignmentCenter;    NSString *oldPrice = @"¥ 999999";   

NSMutableAttributedString添加删除线(中划线)

NSMutableAttributedString *attrString = [[NSMutableAttributedStringalloc] initWithString:@"aaaaaaaa"]; [attrString addAttribute:NSStrikethroughStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] r