-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIPDFMacButton.m
More file actions
115 lines (84 loc) · 2.9 KB
/
IPDFMacButton.m
File metadata and controls
115 lines (84 loc) · 2.9 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// IPDFMacButton.m
// InstaPDF for Mac
//
// Created by mmackh on 14.10.19.
// Copyright © 2019 mackh ag. All rights reserved.
//
#import "IPDFMacButton.h"
@interface NSColor_Catalyst : NSObject
- (instancetype)controlAccentColor;
@end
@interface IPDFMacButton ()
@property (nonatomic) void(^clickHandler)(IPDFMacButton *button);
@end
@implementation IPDFMacButton
+ (instancetype)buttonWithTitle:(NSString *)title clickHandler:(void(^)(IPDFMacButton *button))clickHandler
{
IPDFMacButton *button = [IPDFMacButton buttonWithType:UIButtonTypeSystem];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor labelColor] forState:UIControlStateNormal];
[button addTarget:button action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
button.clickHandler = clickHandler;
[button updateColors];
return button;
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (NSArray<UIKeyCommand *> *)keyCommands
{
return @[[UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(click:)]];
}
- (void)updateColors
{
BOOL night = self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark;
self.titleLabel.textColor = [UIColor labelColor];
self.backgroundColor = night ? [UIColor colorWithRed:0.41 green:0.42 blue:0.44 alpha:1.00] : [UIColor systemBackgroundColor];
self.layer.borderColor = [UIColor tertiaryLabelColor].CGColor;
self.layer.borderWidth = 0.6;
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:1].CGColor;
self.layer.shadowOpacity = 0.9;
self.layer.shadowRadius = 0.5;
self.layer.cornerRadius = 5;
}
- (void)setHighlighted:(BOOL)highlighted
{
//[super setHighlighted:highlighted];
if (highlighted)
{
self.backgroundColor = [NSClassFromString(@"NSColor") performSelector:@selector(controlAccentColor)];
self.titleLabel.textColor = [UIColor whiteColor];
return;
}
[self updateColors];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (CGRectIsEmpty(self.bounds)) return;
CGRect targetRect = CGRectInset(self.bounds, 1, 0);
targetRect.origin.y += 2;
targetRect.size.height -= 2;
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:targetRect cornerRadius:5].CGPath;
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
[self updateColors];
}
- (void)click:(IPDFMacButton *)button
{
__weak typeof(self) weakSelf = self;
if (self.clickHandler) self.clickHandler(weakSelf);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end