-
Notifications
You must be signed in to change notification settings - Fork 1
/
SHCStrikethroughLabel.m
68 lines (54 loc) · 1.41 KB
/
SHCStrikethroughLabel.m
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// SHCStrikethroughLabel.m
// ClearStyle
//
// Created by Colin Eberhardt on 29/08/2012.
// Copyright (c) 2012 Colin Eberhardt. All rights reserved.
//
#import "SHCStrikethroughLabel.h"
#import <QuartzCore/QuartzCore.h>
@implementation SHCStrikethroughLabel
{
bool _strikethrough;
CALayer* _strikethroughLayer;
}
const float STRIKEOUT_THICKNESS = 2.0f;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_strikethroughLayer = [CALayer layer];
_strikethroughLayer.backgroundColor = [[UIColor whiteColor] CGColor];
_strikethroughLayer.hidden = YES;
[self.layer addSublayer:_strikethroughLayer];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self resizeStrikeThrough];
}
-(void)setText:(NSString *)text
{
[super setText:text];
[self resizeStrikeThrough];
}
// resizes the strikethrough layer to match the current label text
-(void)resizeStrikeThrough
{
CGSize textSize = [self.text sizeWithFont:self.font];
_strikethroughLayer.frame = CGRectMake(0, self.bounds.size.height/2,
textSize.width, STRIKEOUT_THICKNESS);
}
#pragma mark - property setters / getters
-(bool) strikethough
{
return _strikethrough;
}
-(void) setStrikethough:(bool)strikethough
{
_strikethrough = strikethough;
_strikethroughLayer.hidden = !strikethough;
}
@end