/
RCCTitleViewHelper.m
235 lines (176 loc) · 7.05 KB
/
RCCTitleViewHelper.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// RCCTitleViewHelper.m
// ReactNativeControllers
//
// Created by Ran Greenberg on 06/09/2016.
// Copyright © 2016 artal. All rights reserved.
//
#import "RCCTitleViewHelper.h"
#import <React/RCTConvert.h>
#import "RCCViewController.h"
#import "RCTHelpers.h"
@implementation RCCTitleView
@end
@interface RCCTitleViewHelper ()
@property (nonatomic, weak) UIViewController *viewController;
@property (nonatomic, weak) UINavigationController *navigationController;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;
@property (nonatomic, strong) id titleImageData;
@property (nonatomic) BOOL isSetSubtitle;
@property (nonatomic, strong) RCCTitleView *titleView;
@end
@implementation RCCTitleViewHelper
- (instancetype)init:(UIViewController*)viewController
navigationController:(UINavigationController*)navigationController
title:(NSString*)title subtitle:(NSString*)subtitle
titleImageData:(id)titleImageData
isSetSubtitle:(BOOL)isSetSubtitle
{
self = [super init];
if (self) {
self.viewController = viewController;
self.navigationController = navigationController;
if(isSetSubtitle){
self.title = viewController.navigationItem.title;
} else {
self.title = [RCCTitleViewHelper validateString:title];
}
self.subtitle = [RCCTitleViewHelper validateString:subtitle];
self.titleImageData = titleImageData;
}
return self;
}
+(NSString*)validateString:(NSString*)string {
if ([string isEqual:[NSNull null]]) {
return nil;
}
return string;
}
-(void)setup:(NSDictionary*)style
{
if (!self.navigationController)
{
return;
}
if ([self.viewController isKindOfClass:[RCCViewController class]]) {
NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:((RCCViewController*)self.viewController).navigatorStyle];
[mergedStyle addEntriesFromDictionary:style];
style = mergedStyle;
}
CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
self.titleView = [[RCCTitleView alloc] initWithFrame:navigationBarBounds];
self.titleView.backgroundColor = [UIColor clearColor];
self.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.titleView.clipsToBounds = YES;
self.viewController.navigationItem.title = self.title;
if ([self isTitleOnly]) {
self.viewController.navigationItem.titleView = nil;
return;
}
if ([self isTitleImage])
{
[self setupTitleImage];
return;
}
if (self.subtitle)
{
self.titleView.subtitleLabel = [self setupSubtitle:style];
}
if (self.title)
{
self.titleView.titleLabel = [self setupTitle:style];
}
[self centerTitleView:navigationBarBounds titleLabel:self.titleView.titleLabel subtitleLabel:self.titleView.subtitleLabel];
self.viewController.navigationItem.titleView = self.titleView;
}
-(BOOL)isTitleOnly
{
BOOL hasNoImageData = [[NSNull null] isEqual:self.titleImageData] || self.titleImageData == nil;
return self.title != nil &&
self.subtitle == nil &&
hasNoImageData;
}
-(BOOL)isTitleImage
{
return self.titleImageData && ![self.titleImageData isEqual:[NSNull null]];
}
-(void)setupTitleImage
{
UIImage *titleImage = [RCTConvert UIImage:self.titleImageData];
UIImageView *imageView = [[UIImageView alloc] initWithImage:titleImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = self.titleView.autoresizingMask;
self.viewController.navigationItem.titleView = imageView;
}
-(void)centerTitleView:(CGRect)navigationBarBounds titleLabel:(UILabel*)titleLabel subtitleLabel:(UILabel*)subtitleLabel
{
CGRect titleViewFrame = navigationBarBounds;
titleViewFrame.size.width = MAX(titleLabel.frame.size.width, subtitleLabel.frame.size.width);;
self.titleView.frame = titleViewFrame;
for (UIView *view in self.titleView.subviews)
{
CGRect viewFrame = view.frame;
viewFrame.size.width = self.titleView.frame.size.width;
viewFrame.origin.x = (self.titleView.frame.size.width - viewFrame.size.width)/2;
view.frame = viewFrame;
}
}
-(UILabel*)setupSubtitle:(NSDictionary*)style
{
CGRect subtitleFrame = self.titleView.frame;
subtitleFrame.size.height /= 2;
subtitleFrame.origin.y = subtitleFrame.size.height;
UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:subtitleFrame];
subtitleLabel.textAlignment = NSTextAlignmentCenter;
subtitleLabel.backgroundColor = [UIColor clearColor];
subtitleLabel.autoresizingMask = self.titleView.autoresizingMask;
NSMutableDictionary *subtitleAttributes = [RCTHelpers textAttributesFromDictionary:style withPrefix:@"navBarSubtitle" baseFont:[UIFont systemFontOfSize:14.f]];
[subtitleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.subtitle attributes:subtitleAttributes]];
CGSize labelSize = [subtitleLabel.text sizeWithAttributes:subtitleAttributes];
CGRect labelframe = subtitleLabel.frame;
labelframe.size = labelSize;
subtitleLabel.frame = labelframe;
[subtitleLabel sizeToFit];
[self.titleView addSubview:subtitleLabel];
return subtitleLabel;
}
-(UILabel*)setupTitle:(NSDictionary*)style
{
CGRect titleFrame = self.titleView.frame;
if (self.subtitle)
{
titleFrame.size.height /= 2;
}
UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleFrame];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.autoresizingMask = self.titleView.autoresizingMask;
UIFont *titleFont = [UIFont boldSystemFontOfSize:17.f];
id fontSize = style[@"navBarTitleFontSize"];
if (fontSize) {
CGFloat fontSizeFloat = [RCTConvert CGFloat:fontSize];
titleFont = [UIFont boldSystemFontOfSize:fontSizeFloat];
}
titleLabel.font = titleFont;
NSMutableDictionary *titleAttributes = [RCTHelpers textAttributesFromDictionary:style withPrefix:@"navBarTitle" baseFont:[UIFont systemFontOfSize:14.f]];
[titleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.title attributes:titleAttributes]];
CGSize labelSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:titleFont}];
CGRect labelframe = titleLabel.frame;
labelframe.size = labelSize;
titleLabel.frame = labelframe;
if (!self.subtitle)
{
titleLabel.center = self.titleView.center;
}
id navBarTextColor = style[@"navBarTextColor"];
if (navBarTextColor)
{
UIColor *color = navBarTextColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarTextColor] : nil;
titleLabel.textColor = color;
}
[titleLabel sizeToFit];
[self.titleView addSubview:titleLabel];
return titleLabel;
}
@end