123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // RCCTitleViewHelper.m
- // ReactNativeControllers
- //
- // Created by Ran Greenberg on 06/09/2016.
- // Copyright © 2016 artal. All rights reserved.
- //
-
- #import "RCCTitleViewHelper.h"
- #import "RCTConvert.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, strong) RCCTitleView *titleView;
-
- @end
-
-
- @implementation RCCTitleViewHelper
-
- - (instancetype)init:(UIViewController*)viewController
- navigationController:(UINavigationController*)navigationController
- title:(NSString*)title subtitle:(NSString*)subtitle
- titleImageData:(id)titleImageData
- {
- self = [super init];
- if (self) {
- self.viewController = viewController;
- self.navigationController = navigationController;
- self.title = title;
- self.subtitle = subtitle;
- self.titleImageData = titleImageData;
- }
- return self;
- }
-
- -(void)setup:(NSDictionary*)style
- {
- if (!self.navigationController)
- {
- return;
- }
-
- 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.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
- {
- return self.title && !self.subtitle && !self.titleImageData;
- }
-
-
- -(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;
-
- [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;
- }
-
- [self.titleView addSubview:titleLabel];
-
- return titleLabel;
- }
-
-
- @end
|