划掉效果的label


1.設置label的attributedText ,eg:

let price_attr = NSAttributedString(string: "500", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])

        label.attributedText = price_attr

2.自定義label

#import <UIKit/UIKit.h>


@interface MyLabel2 : UILabel{

      BOOL isWithStrikeThrough;

}

@property (nonatomic,assign)BOOL isWithStrikeThrough;

@end



//

//  MyLabel2.m

//  missbetterc

//

//  Created by sip on 15/5/8.

//  Copyright (c) 2015 qiqugou. All rights reserved.

//


#import "MyLabel2.h"

#import "setting.h"

@implementation MyLabel2


@synthesize isWithStrikeThrough;


- (void)drawRect:(CGRect)rect

{

    if (isWithStrikeThrough)

    {

        

        CGContextRef c =UIGraphicsGetCurrentContext();

        

        CGFloat red[4] = {1.0f,1.0f,1.0f,1.f};//白色

        //CGFloat black[4] = {0.0f, 0.0f, 0.0f, 0.5f};//黑色

        CGContextSetStrokeColor(c, red);

        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 + rect.size.width, halfWayUp);//结束点

        //画斜线

//        CGContextMoveToPoint(c, rect.origin.x, rect.origin.y+5 );

//        CGContextAddLineToPoint(c, (rect.origin.x + rect.size.width)*0.5, rect.origin.y+rect.size.height-5); //斜线

        CGContextStrokePath(c);

    }

    

    [superdrawRect:rect];

}



@end



PS:也可以通过设置String属性的方式生成删除线,

例如:

 NSDictionary *subStrAttribute1 =@{

                                       NSForegroundColorAttributeName:[UIColorredColor],

                                       NSStrikethroughStyleAttributeName:@2

                                       };

    NSDictionary *SUBsTRaTTRIBUTE2 =@{

                                       NSForegroundColorAttributeName:[UIColorgreenColor]

                                       };

    NSString *strDisplayText3 =@"Red and Green";

    NSMutableAttributedString *attributedText3 = [[NSMutableAttributedStringalloc]

                                                  initWithString:strDisplayText3 ];

    [attributedText3 setAttributes:subStrAttribute1range:NSMakeRange(0,3)];

    [attributedText3 setAttributes:SUBsTRaTTRIBUTE2range:NSMakeRange(8,5)];

    self.myLabel.attributedText = attributedText3;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值