react-native-navigation的迁移库

RCCTitleViewHelper.m 6.6KB

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