react-native-navigation的迁移库

RCCTitleViewHelper.m 6.9KB

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