標籤:
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)];
[email protected]"wo ai ni ";
label.strikeThroughEnabled=YES;
[self.view addSubview:label];
搞定了
ios怎麼實現帶刪除線的label