uilabel 的删除线 strikelable

Method One

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (UILabel *)strikeLalbe {
if (_strikeLalbe == nil) {
_strikeLalbe = [[UILabel alloc] init];
[_strikeLalbe setFont:SYSFONT(12.0f)];
[_strikeLalbe setTextColor:AYCOLOR_SUBTITLE];
[_strikeLalbe setTextAlignment:NSTextAlignmentRight];
[_strikeLalbe setText:@"8"];
NSUInteger length = [_strikeLalbe.text length];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:_strikeLalbe.text];
[attri addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)
range:NSMakeRange(0, length)];
[attri addAttribute:NSStrikethroughColorAttributeName
value:AYCOLOR_SUBTITLE
range:NSMakeRange(0, length)];
[_strikeLalbe setAttributedText:attri];
}
return _strikeLalbe;
}

Method Two

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
- (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);
}
}