react-native-navigation的迁移库

RCCTitleViewHelper.m 6.1KB

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