react-native-navigation的迁移库

RCCTitleViewHelper.m 6.1KB

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