|
@@ -0,0 +1,193 @@
|
|
1
|
+
|
|
2
|
+#import "RNNTitleViewHelper.h"
|
|
3
|
+#import <React/RCTConvert.h>
|
|
4
|
+#import "RCTHelpers.h"
|
|
5
|
+
|
|
6
|
+@implementation RNNTitleView
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+@end
|
|
10
|
+
|
|
11
|
+@interface RNNTitleViewHelper ()
|
|
12
|
+
|
|
13
|
+@property (nonatomic, weak) UIViewController *viewController;
|
|
14
|
+
|
|
15
|
+@property (nonatomic, strong) NSString *title;
|
|
16
|
+@property (nonatomic, strong) NSString *subtitle;
|
|
17
|
+@property (nonatomic, strong) id titleImageData;
|
|
18
|
+@property (nonatomic) BOOL isSetSubtitle;
|
|
19
|
+
|
|
20
|
+@property (nonatomic, strong) RNNTitleView *titleView;
|
|
21
|
+
|
|
22
|
+@end
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+@implementation RNNTitleViewHelper
|
|
26
|
+
|
|
27
|
+- (instancetype)init:(UIViewController*)viewController
|
|
28
|
+ title:(NSString*)title subtitle:(NSString*)subtitle
|
|
29
|
+ titleImageData:(id)titleImageData
|
|
30
|
+ isSetSubtitle:(BOOL)isSetSubtitle {
|
|
31
|
+ self = [super init];
|
|
32
|
+ if (self) {
|
|
33
|
+ self.viewController = viewController;
|
|
34
|
+ if (isSetSubtitle){
|
|
35
|
+ self.title = viewController.navigationItem.title;
|
|
36
|
+ } else {
|
|
37
|
+ self.title = [RNNTitleViewHelper validateString:title];
|
|
38
|
+ }
|
|
39
|
+ self.subtitle = [RNNTitleViewHelper validateString:subtitle];
|
|
40
|
+ self.titleImageData = titleImageData;
|
|
41
|
+ }
|
|
42
|
+ return self;
|
|
43
|
+}
|
|
44
|
+
|
|
45
|
++(NSString*)validateString:(NSString*)string {
|
|
46
|
+ if ([string isEqual:[NSNull null]]) {
|
|
47
|
+ return nil;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ return string;
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+-(void)setup:(RNNTitleOptions*)style {
|
|
54
|
+
|
|
55
|
+ CGRect navigationBarBounds = self.viewController.navigationController.navigationBar.bounds;
|
|
56
|
+
|
|
57
|
+ self.titleView = [[RNNTitleView alloc] initWithFrame:navigationBarBounds];
|
|
58
|
+ self.titleView.backgroundColor = [UIColor clearColor];
|
|
59
|
+ self.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
|
|
60
|
+ self.titleView.clipsToBounds = YES;
|
|
61
|
+
|
|
62
|
+ self.viewController.navigationItem.title = self.title;
|
|
63
|
+
|
|
64
|
+ if ([self isTitleOnly]) {
|
|
65
|
+ self.viewController.navigationItem.titleView = nil;
|
|
66
|
+ return;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ if ([self isTitleImage]) {
|
|
70
|
+ [self setupTitleImage];
|
|
71
|
+ return;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ if (self.subtitle) {
|
|
75
|
+ self.titleView.subtitleLabel = [self setupSubtitle:style];
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ if (self.title) {
|
|
79
|
+ self.titleView.titleLabel = [self setupTitle:style];
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ [self centerTitleView:navigationBarBounds titleLabel:self.titleView.titleLabel subtitleLabel:self.titleView.subtitleLabel];
|
|
83
|
+
|
|
84
|
+ self.viewController.navigationItem.titleView = self.titleView;
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+-(BOOL)isTitleOnly {
|
|
89
|
+ return self.title && !self.subtitle && !self.titleImageData;
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+-(BOOL)isTitleImage {
|
|
94
|
+ return self.titleImageData && ![self.titleImageData isEqual:[NSNull null]];
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+-(void)setupTitleImage {
|
|
99
|
+ UIImage *titleImage = [RCTConvert UIImage:self.titleImageData];
|
|
100
|
+ UIImageView *imageView = [[UIImageView alloc] initWithImage:titleImage];
|
|
101
|
+ imageView.contentMode = UIViewContentModeScaleAspectFit;
|
|
102
|
+ imageView.autoresizingMask = self.titleView.autoresizingMask;
|
|
103
|
+
|
|
104
|
+ self.viewController.navigationItem.titleView = imageView;
|
|
105
|
+}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+-(void)centerTitleView:(CGRect)navigationBarBounds titleLabel:(UILabel*)titleLabel subtitleLabel:(UILabel*)subtitleLabel
|
|
109
|
+{
|
|
110
|
+ CGRect titleViewFrame = navigationBarBounds;
|
|
111
|
+ titleViewFrame.size.width = MAX(titleLabel.frame.size.width, subtitleLabel.frame.size.width);;
|
|
112
|
+ self.titleView.frame = titleViewFrame;
|
|
113
|
+
|
|
114
|
+ for (UIView *view in self.titleView.subviews) {
|
|
115
|
+ CGRect viewFrame = view.frame;
|
|
116
|
+ viewFrame.size.width = self.titleView.frame.size.width;
|
|
117
|
+ viewFrame.origin.x = (self.titleView.frame.size.width - viewFrame.size.width)/2;
|
|
118
|
+ view.frame = viewFrame;
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+-(UILabel*)setupSubtitle:(RNNTitleOptions*)style {
|
|
125
|
+ CGRect subtitleFrame = self.titleView.frame;
|
|
126
|
+ subtitleFrame.size.height /= 2;
|
|
127
|
+ subtitleFrame.origin.y = subtitleFrame.size.height;
|
|
128
|
+
|
|
129
|
+ UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:subtitleFrame];
|
|
130
|
+ subtitleLabel.textAlignment = NSTextAlignmentCenter;
|
|
131
|
+ subtitleLabel.backgroundColor = [UIColor clearColor];
|
|
132
|
+ subtitleLabel.autoresizingMask = self.titleView.autoresizingMask;
|
|
133
|
+
|
|
134
|
+ [subtitleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.subtitle attributes:style.fontAttributes]];
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+ CGSize labelSize = [subtitleLabel.text sizeWithAttributes:style.fontAttributes];
|
|
138
|
+ CGRect labelframe = subtitleLabel.frame;
|
|
139
|
+ labelframe.size = labelSize;
|
|
140
|
+ subtitleLabel.frame = labelframe;
|
|
141
|
+ [subtitleLabel sizeToFit];
|
|
142
|
+
|
|
143
|
+ [self.titleView addSubview:subtitleLabel];
|
|
144
|
+
|
|
145
|
+ return subtitleLabel;
|
|
146
|
+}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+-(UILabel*)setupTitle:(RNNTitleOptions*)style {
|
|
150
|
+ CGRect titleFrame = self.titleView.frame;
|
|
151
|
+ if (self.subtitle) {
|
|
152
|
+ titleFrame.size.height /= 2;
|
|
153
|
+ }
|
|
154
|
+ UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleFrame];
|
|
155
|
+ titleLabel.textAlignment = NSTextAlignmentCenter;
|
|
156
|
+ titleLabel.backgroundColor = [UIColor clearColor];
|
|
157
|
+
|
|
158
|
+ titleLabel.autoresizingMask = self.titleView.autoresizingMask;
|
|
159
|
+
|
|
160
|
+ UIFont *titleFont = [UIFont boldSystemFontOfSize:17.f];
|
|
161
|
+
|
|
162
|
+ id fontSize = style.subtitleFontSize;
|
|
163
|
+ if (fontSize) {
|
|
164
|
+ CGFloat fontSizeFloat = [RCTConvert CGFloat:fontSize];
|
|
165
|
+ titleFont = [UIFont boldSystemFontOfSize:fontSizeFloat];
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ [titleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.title attributes:style.subtitleFontAttributes]];
|
|
169
|
+
|
|
170
|
+ CGSize labelSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:titleFont}];
|
|
171
|
+ CGRect labelframe = titleLabel.frame;
|
|
172
|
+ labelframe.size = labelSize;
|
|
173
|
+ titleLabel.frame = labelframe;
|
|
174
|
+
|
|
175
|
+ if (!self.subtitle) {
|
|
176
|
+ titleLabel.center = self.titleView.center;
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ id navBarTextColor = style.subtitleColor;
|
|
180
|
+ if (navBarTextColor) {
|
|
181
|
+ UIColor *color = navBarTextColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarTextColor] : nil;
|
|
182
|
+ titleLabel.textColor = color;
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ [titleLabel sizeToFit];
|
|
186
|
+ [self.titleView addSubview:titleLabel];
|
|
187
|
+
|
|
188
|
+ return titleLabel;
|
|
189
|
+}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+@end
|
|
193
|
+
|