- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.strikeThroughEnabled = YES;
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:rect];
CGSize textSize = [[self text] sizeWithFont:[self font]];
CGFloat strikeWidth = textSize.width;
CGRect lineRect;
if ([self textAlignment] == NSTextAlignmentRight) {
lineRect = CGRectMake(rect.size.width - strikeWidth,
rect.size.height/2,
strikeWidth,
1);
}else if ([self textAlignment] == NSTextAlignmentCenter) {
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 (self.strikeThroughEnabled) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
[self strikeThroughColor].CGColor);
CGContextFillRect(context,
lineRect);
}
}
近期评论