react-native-navigation的迁移库

RCCTitleViewHelper.m 6.4KB

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