forked from atomton/ATMHud
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathATMTextLayer.m
58 lines (48 loc) · 1.56 KB
/
ATMTextLayer.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
/*
* ATMTextLayer.m
* ATMHud
*
* Created by Marcel Müller on 2011-03-01.
* Copyright (c) 2010-2011, Marcel Müller (atomcraft)
* Copyright (c) 2012-2014, David Hoerl
* All rights reserved.
*
* https://github.com/atomton/ATMHud (original)
*/
#import "ATMTextLayer.h"
//#define DROP_SHADOW
@implementation ATMTextLayer
+ (BOOL)needsDisplayForKey:(NSString *)key
{
if ([key isEqualToString:@"caption"]) {
return YES;
} else {
return [super needsDisplayForKey:key];
}
}
- (instancetype)initWithLayer:(id)layer
{
if ((self = [super init])) {
self.caption = @"";
}
return self;
}
- (void)drawInContext:(CGContextRef)ctx
{
UIGraphicsPushContext(ctx); // Makes this contest the current context
CGRect f = self.bounds;
#ifdef DROP_SHADOW
CGRect s = f;
#endif
f.origin.y -= 1; // seems weird, but the text looks a bit better being just a pixel higher! This is how the original code worked.
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentCenter;
#ifdef DROP_SHADOW
[_caption drawInRect:s withAttributes:@{ NSFontAttributeName : font, NSParagraphStyleAttributeName : paragraphStyle, NSForegroundColorAttributeName : [UIColor grayColor]}];
#endif
[_caption drawInRect:f withAttributes:@{ NSFontAttributeName : font, NSParagraphStyleAttributeName : paragraphStyle, NSForegroundColorAttributeName : [UIColor blackColor]}];
UIGraphicsPopContext();
}
@end