react-native-navigation的迁移库

RCCTitleViewHelper.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. BOOL hasNoImageData = [[NSNull null] isEqual:self.titleImageData] || self.titleImageData == nil;
  90. return self.title != nil &&
  91. self.subtitle == nil &&
  92. hasNoImageData;
  93. }
  94. -(BOOL)isTitleImage
  95. {
  96. return self.titleImageData && ![self.titleImageData isEqual:[NSNull null]];
  97. }
  98. -(void)setupTitleImage
  99. {
  100. UIImage *titleImage = [RCTConvert UIImage:self.titleImageData];
  101. UIImageView *imageView = [[UIImageView alloc] initWithImage:titleImage];
  102. imageView.contentMode = UIViewContentModeScaleAspectFit;
  103. imageView.autoresizingMask = self.titleView.autoresizingMask;
  104. self.viewController.navigationItem.titleView = imageView;
  105. }
  106. -(void)centerTitleView:(CGRect)navigationBarBounds titleLabel:(UILabel*)titleLabel subtitleLabel:(UILabel*)subtitleLabel
  107. {
  108. CGRect titleViewFrame = navigationBarBounds;
  109. titleViewFrame.size.width = MAX(titleLabel.frame.size.width, subtitleLabel.frame.size.width);;
  110. self.titleView.frame = titleViewFrame;
  111. for (UIView *view in self.titleView.subviews)
  112. {
  113. CGRect viewFrame = view.frame;
  114. viewFrame.size.width = self.titleView.frame.size.width;
  115. viewFrame.origin.x = (self.titleView.frame.size.width - viewFrame.size.width)/2;
  116. view.frame = viewFrame;
  117. }
  118. }
  119. -(UILabel*)setupSubtitle:(NSDictionary*)style
  120. {
  121. CGRect subtitleFrame = self.titleView.frame;
  122. subtitleFrame.size.height /= 2;
  123. subtitleFrame.origin.y = subtitleFrame.size.height;
  124. UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:subtitleFrame];
  125. subtitleLabel.textAlignment = NSTextAlignmentCenter;
  126. subtitleLabel.backgroundColor = [UIColor clearColor];
  127. subtitleLabel.autoresizingMask = self.titleView.autoresizingMask;
  128. NSMutableDictionary *subtitleAttributes = [RCTHelpers textAttributesFromDictionary:style withPrefix:@"navBarSubtitle" baseFont:[UIFont systemFontOfSize:14.f]];
  129. [subtitleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.subtitle attributes:subtitleAttributes]];
  130. CGSize labelSize = [subtitleLabel.text sizeWithAttributes:subtitleAttributes];
  131. CGRect labelframe = subtitleLabel.frame;
  132. labelframe.size = labelSize;
  133. subtitleLabel.frame = labelframe;
  134. [subtitleLabel sizeToFit];
  135. [self.titleView addSubview:subtitleLabel];
  136. return subtitleLabel;
  137. }
  138. -(UILabel*)setupTitle:(NSDictionary*)style
  139. {
  140. CGRect titleFrame = self.titleView.frame;
  141. if (self.subtitle)
  142. {
  143. titleFrame.size.height /= 2;
  144. }
  145. UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleFrame];
  146. titleLabel.textAlignment = NSTextAlignmentCenter;
  147. titleLabel.backgroundColor = [UIColor clearColor];
  148. titleLabel.autoresizingMask = self.titleView.autoresizingMask;
  149. UIFont *titleFont = [UIFont boldSystemFontOfSize:17.f];
  150. id fontSize = style[@"navBarTitleFontSize"];
  151. if (fontSize) {
  152. CGFloat fontSizeFloat = [RCTConvert CGFloat:fontSize];
  153. titleFont = [UIFont boldSystemFontOfSize:fontSizeFloat];
  154. }
  155. titleLabel.font = titleFont;
  156. NSMutableDictionary *titleAttributes = [RCTHelpers textAttributesFromDictionary:style withPrefix:@"navBarTitle" baseFont:[UIFont systemFontOfSize:14.f]];
  157. [titleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.title attributes:titleAttributes]];
  158. CGSize labelSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:titleFont}];
  159. CGRect labelframe = titleLabel.frame;
  160. labelframe.size = labelSize;
  161. titleLabel.frame = labelframe;
  162. if (!self.subtitle)
  163. {
  164. titleLabel.center = self.titleView.center;
  165. }
  166. id navBarTextColor = style[@"navBarTextColor"];
  167. if (navBarTextColor)
  168. {
  169. UIColor *color = navBarTextColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarTextColor] : nil;
  170. titleLabel.textColor = color;
  171. }
  172. [titleLabel sizeToFit];
  173. [self.titleView addSubview:titleLabel];
  174. return titleLabel;
  175. }
  176. @end