react-native-navigation的迁移库

RNNTitleViewHelper.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.fontAttributes]];
  96. CGSize labelSize = [subtitleLabel.text sizeWithAttributes:style.fontAttributes];
  97. CGRect labelframe = subtitleLabel.frame;
  98. labelframe.size = labelSize;
  99. subtitleLabel.frame = labelframe;
  100. [subtitleLabel sizeToFit];
  101. [self.titleView addSubview:subtitleLabel];
  102. return subtitleLabel;
  103. }
  104. -(UILabel*)setupTitle:(RNNTitleOptions*)style {
  105. CGRect titleFrame = self.titleView.frame;
  106. if (self.subtitle) {
  107. titleFrame.size.height /= 2;
  108. }
  109. UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleFrame];
  110. titleLabel.textAlignment = NSTextAlignmentCenter;
  111. titleLabel.backgroundColor = [UIColor clearColor];
  112. titleLabel.autoresizingMask = self.titleView.autoresizingMask;
  113. UIFont *titleFont = [UIFont boldSystemFontOfSize:17.f];
  114. id fontSize = style.subtitleFontSize;
  115. if (fontSize) {
  116. CGFloat fontSizeFloat = [RCTConvert CGFloat:fontSize];
  117. titleFont = [UIFont boldSystemFontOfSize:fontSizeFloat];
  118. }
  119. [titleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.title attributes:style.subtitleFontAttributes]];
  120. CGSize labelSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:titleFont}];
  121. CGRect labelframe = titleLabel.frame;
  122. labelframe.size = labelSize;
  123. titleLabel.frame = labelframe;
  124. if (!self.subtitle) {
  125. titleLabel.center = self.titleView.center;
  126. }
  127. id navBarTextColor = style.subtitleColor;
  128. if (navBarTextColor) {
  129. UIColor *color = navBarTextColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarTextColor] : nil;
  130. titleLabel.textColor = color;
  131. }
  132. [titleLabel sizeToFit];
  133. [self.titleView addSubview:titleLabel];
  134. return titleLabel;
  135. }
  136. @end