-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDINNotificationView.m
More file actions
159 lines (137 loc) · 6.87 KB
/
DINNotificationView.m
File metadata and controls
159 lines (137 loc) · 6.87 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#import "DINNotificationView.h"
@implementation DINNotificationView
- (instancetype)initWithTitle:(NSString *)title
message:(NSString *)message
appName:(NSString *)appName
icon:(UIImage *)icon
style:(DINNotificationStyle)style {
if (self = [super initWithFrame:CGRectZero]) {
switch (style) {
case DINNotificationStyleCompact:
[self buildCompactWithTitle:title appName:appName icon:icon];
break;
case DINNotificationStyleMinimal:
[self buildMinimalWithAppName:appName icon:icon];
break;
default:
[self buildStandardWithTitle:title message:message appName:appName icon:icon];
break;
}
}
return self;
}
#pragma mark - Standard: [Icon 48] Title + Message
- (void)buildStandardWithTitle:(NSString *)title
message:(NSString *)message
appName:(NSString *)appName
icon:(UIImage *)icon {
_iconImageView = [[UIImageView alloc] init];
_iconImageView.contentMode = UIViewContentModeScaleAspectFill;
_iconImageView.layer.cornerRadius = 13;
_iconImageView.clipsToBounds = YES;
_iconImageView.image = icon;
_iconImageView.translatesAutoresizingMaskIntoConstraints = NO;
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.numberOfLines = 1;
_titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
NSString *displayTitle = (title.length > 0) ? title : appName;
_titleLabel.text = displayTitle ?: @"Notification";
_messageLabel = [[UILabel alloc] init];
_messageLabel.font = [UIFont systemFontOfSize:14];
_messageLabel.textColor = [UIColor colorWithWhite:1.0 alpha:0.55];
_messageLabel.numberOfLines = 1;
_messageLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
if (message.length > 0) {
_messageLabel.text = message;
} else {
_messageLabel.hidden = YES;
}
[self addSubview:_iconImageView];
[self addSubview:_titleLabel];
[self addSubview:_messageLabel];
// AirDrop-style layout: icon centered, title bottom near icon centerY, message below
[NSLayoutConstraint activateConstraints:@[
// Icon: 56x56, vertically centered
[_iconImageView.widthAnchor constraintEqualToConstant:56],
[_iconImageView.heightAnchor constraintEqualToConstant:56],
[_iconImageView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[_iconImageView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
// Title: leading to icon
[_titleLabel.leadingAnchor constraintEqualToAnchor:_iconImageView.trailingAnchor constant:12],
[_titleLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor],
// Message: bottom aligns with icon bottom
[_messageLabel.bottomAnchor constraintEqualToAnchor:_iconImageView.bottomAnchor],
[_messageLabel.leadingAnchor constraintEqualToAnchor:_titleLabel.leadingAnchor],
[_messageLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor],
// Title: just above message
[_titleLabel.bottomAnchor constraintEqualToAnchor:_messageLabel.topAnchor constant:0],
]];
}
#pragma mark - Compact: [Icon 32] Title only
- (void)buildCompactWithTitle:(NSString *)title
appName:(NSString *)appName
icon:(UIImage *)icon {
_iconImageView = [[UIImageView alloc] init];
_iconImageView.contentMode = UIViewContentModeScaleAspectFill;
_iconImageView.layer.cornerRadius = 8;
_iconImageView.clipsToBounds = YES;
_iconImageView.image = icon;
_iconImageView.translatesAutoresizingMaskIntoConstraints = NO;
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.numberOfLines = 1;
_titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
NSString *displayTitle = (title.length > 0) ? title : appName;
_titleLabel.text = displayTitle ?: @"Notification";
UIStackView *hStack = [[UIStackView alloc]
initWithArrangedSubviews:@[_iconImageView, _titleLabel]];
hStack.axis = UILayoutConstraintAxisHorizontal;
hStack.spacing = 10;
hStack.alignment = UIStackViewAlignmentCenter;
hStack.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:hStack];
[NSLayoutConstraint activateConstraints:@[
[_iconImageView.widthAnchor constraintEqualToConstant:32],
[_iconImageView.heightAnchor constraintEqualToConstant:32],
[hStack.topAnchor constraintEqualToAnchor:self.topAnchor],
[hStack.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[hStack.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
[hStack.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
]];
}
#pragma mark - Minimal: Large centered icon + app name
- (void)buildMinimalWithAppName:(NSString *)appName
icon:(UIImage *)icon {
_iconImageView = [[UIImageView alloc] init];
_iconImageView.contentMode = UIViewContentModeScaleAspectFill;
_iconImageView.layer.cornerRadius = 14;
_iconImageView.clipsToBounds = YES;
_iconImageView.image = icon;
_iconImageView.translatesAutoresizingMaskIntoConstraints = NO;
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightMedium];
_titleLabel.textColor = [UIColor colorWithWhite:1.0 alpha:0.7];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.numberOfLines = 1;
_titleLabel.text = appName ?: @"App";
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_iconImageView];
[self addSubview:_titleLabel];
[NSLayoutConstraint activateConstraints:@[
[_iconImageView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
[_iconImageView.topAnchor constraintEqualToAnchor:self.topAnchor],
[_iconImageView.widthAnchor constraintEqualToConstant:56],
[_iconImageView.heightAnchor constraintEqualToConstant:56],
[_titleLabel.topAnchor constraintEqualToAnchor:_iconImageView.bottomAnchor constant:4],
[_titleLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
[_titleLabel.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.leadingAnchor],
[_titleLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor],
[_titleLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
]];
}
@end