标签:
1.自己新建一个类名字为StrikeLabel,是UILabel的子类;
2.在StrikeLabel.h里@property(nonatomic)BOOL strikeThroughEnabled;
在StrikeLabel.m里
- (void)drawRect:(CGRect)rect{
[super drawTextInRect:rect];
CGSize textSize = [[self text] sizeWithFont:[self font]];
CGFloat strikeWidth = textSize.width;
CGRect lineRect;
if ([self textAlignment] == UITextAlignmentRight) {
lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth, 1);
} else if ([self textAlignment] == UITextAlignmentCenter) {
lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2, strikeWidth, 1);
} else {
lineRect = CGRectMake(0, rect.size.height/2, strikeWidth, 1);
}
if (_strikeThroughEnabled) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context, lineRect);
}}
3.在需要声明label的时候
StrikeLabel *label=[[StrikeLabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
label.text=@"wo ai ni ";
label.strikeThroughEnabled=YES;
[self.view addSubview:label];
搞定了
标签:
原文地址:http://www.cnblogs.com/huangzs/p/4463115.html