react-native-navigation的迁移库

RNNTitleViewHelper.m 5.5KB

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