首先生成一個繼承與UILabel的類SFPStrikeThroughAndUnderLineLabel數組
一,在.h文件裏ide
@interface SFPStrikeThroughAndUnderLineLabel : UILabelatom
{spa
BOOL _isWithStrikeThrough; code
}orm
@property (nonatomic, assign) BOOL isWithStrikeThrough; //控制是否顯示刪除線對象
@property (nonatomic, assign) CGFloat strikeThroughLineWidth;//設置刪除線的寬度繼承
@property (nonatomic, retain) NSArray *strikeThroughRGBAlphaArray;//設置刪除線的顏色,數組裏面的元素分別是RGB以及alpha值也就是說該數組有四個元素animation
@endit
二,在.m文件裏
#import "SFPStrikeThroughAndUnderLineLabel.h"
@implementation SFPStrikeThroughAndUnderLineLabel
@synthesize isWithStrikeThrough = _isWithStrikeThrough;
@synthesize strikeThroughRGBAlphaArray = _strikeThroughRGBAlphaArray;
@synthesize strikeThroughLineWidth = _strikeThroughLineWidth;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.strikeThroughRGBAlphaArray = [[NSArrayalloc]initWithObjects:@"0",@"0",@"0",@"1", nil];//默認刪除線的顏色是黑色切實不透明的,給個默認值,創建對象時該屬性能夠沒必要賦值
self.strikeThroughLineWidth = 1;//默認刪除線的寬度是1pix
}
returnself;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.isWithStrikeThrough)//顯示刪除線
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat color[4] = {[[self.strikeThroughRGBAlphaArrayobjectAtIndex:0] floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:1] floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:2] floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:3] floatValue]};
CGContextSetStrokeColor(c, color);
CGContextSetLineWidth(c, self.strikeThroughLineWidth);
CGContextBeginPath(c);
CGFloat halfWayUp = (self.bounds.size.height - self.bounds.origin.y) / 2.0;
CGContextMoveToPoint(c, self.bounds.origin.x, halfWayUp );
CGContextAddLineToPoint(c, self.bounds.origin.x + self.bounds.size.width, halfWayUp);
CGContextStrokePath(c);
}
[super drawRect:rect];
}
三,使用:添加SFPStrikeThroughAndUnderLineLabel.h文件
SFPStrikeThroughAndUnderLineLabel *oneLabel = [[SFPStrikeThroughAndUnderLineLabelalloc]initWithFrame:CGRectMake(20, 20, 100, 200)];
oneLabel.backgroundColor = [UIColorwhiteColor];
oneLabel.numberOfLines = 0;
oneLabel.text = @"朱冬玲,我愛你一萬年;朱冬玲,我愛你一萬年;朱冬玲,我愛你一萬年;朱冬玲,我愛你一萬年;朱冬玲,我愛你一萬年";
oneLabel.isWithStrikeThrough = YES;
// 下面這兩個屬性可設可不設值,他們設的都有默認值
// oneLabel.rgbAlphaArray = [NSArray arrayWithObjects:@"255",@"255",@"0",@"1", nil];
// oneLabel.strikeThroughLineWidth = 20;
[self.view addSubview:oneLabel];
注意:這是給UILabel整個控件加刪除線(一個label只會有一條刪除線),而不是給label的text加刪除線