react-native-navigation的迁移库

RNNTitleViewHelper.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #import "RNNTitleViewHelper.h"
  2. #import <React/RCTConvert.h>
  3. #import "RCTHelpers.h"
  4. #import "RNNFontAttributesCreator.h"
  5. @implementation RNNTitleView
  6. - (void)layoutSubviews {
  7. CGFloat heightSum = _titleLabel.frame.size.height + _subtitleLabel.frame.size.height;
  8. CGFloat yOffset = (self.frame.size.height - heightSum) / 2;
  9. [_titleLabel setFrame:CGRectMake(0, yOffset, self.frame.size.width, _titleLabel.frame.size.height)];
  10. [_subtitleLabel setFrame:CGRectMake(0, yOffset+_titleLabel.frame.size.height, self.frame.size
  11. .width, _subtitleLabel.frame.size.height)];
  12. }
  13. @end
  14. @interface RNNTitleViewHelper ()
  15. @property (nonatomic, weak) UIViewController *viewController;
  16. @property (nonatomic, strong) RNNTitleView *titleView;
  17. @end
  18. @implementation RNNTitleViewHelper
  19. - (instancetype)initWithTitleViewOptions:(RNNTitleOptions*)titleOptions
  20. subTitleOptions:(RNNSubtitleOptions*)subtitleOptions
  21. viewController:(UIViewController*)viewController {
  22. self = [super init];
  23. if (self) {
  24. self.viewController = viewController;
  25. self.titleOptions = titleOptions;
  26. self.subtitleOptions = subtitleOptions;
  27. }
  28. return self;
  29. }
  30. - (NSString *)title {
  31. return [self.titleOptions.text getWithDefaultValue:nil];
  32. }
  33. - (NSString *)subtitle {
  34. return [self.subtitleOptions.text getWithDefaultValue:nil];
  35. }
  36. +(NSString*)validateString:(NSString*)string {
  37. if ([string isEqual:[NSNull null]]) {
  38. return nil;
  39. }
  40. return string;
  41. }
  42. -(void)setup {
  43. CGRect navigationBarBounds = self.viewController.navigationController.navigationBar.bounds;
  44. self.titleView = [[RNNTitleView alloc] initWithFrame:navigationBarBounds];
  45. self.titleView.backgroundColor = [UIColor clearColor];
  46. self.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
  47. self.titleView.clipsToBounds = YES;
  48. if (self.subtitle) {
  49. self.titleView.subtitleLabel = [self setupSubtitle];
  50. }
  51. if (self.title) {
  52. self.titleView.titleLabel = [self setupTitle];
  53. }
  54. [self centerTitleView:navigationBarBounds titleLabel:self.titleView.titleLabel subtitleLabel:self.titleView.subtitleLabel];
  55. self.viewController.navigationItem.titleView = self.titleView;
  56. }
  57. -(void)centerTitleView:(CGRect)navigationBarBounds titleLabel:(UILabel*)titleLabel subtitleLabel:(UILabel*)subtitleLabel
  58. {
  59. CGRect titleViewFrame = navigationBarBounds;
  60. titleViewFrame.size.width = MAX(titleLabel.frame.size.width, subtitleLabel.frame.size.width);;
  61. self.titleView.frame = titleViewFrame;
  62. for (UIView *view in self.titleView.subviews) {
  63. CGRect viewFrame = view.frame;
  64. viewFrame.size.width = self.titleView.frame.size.width;
  65. viewFrame.origin.x = (self.titleView.frame.size.width - viewFrame.size.width)/2;
  66. view.frame = viewFrame;
  67. }
  68. }
  69. -(UILabel*)setupSubtitle {
  70. CGRect subtitleFrame = self.titleView.frame;
  71. subtitleFrame.size.height /= 2;
  72. subtitleFrame.origin.y = subtitleFrame.size.height;
  73. UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:subtitleFrame];
  74. subtitleLabel.textAlignment = NSTextAlignmentCenter;
  75. subtitleLabel.backgroundColor = [UIColor clearColor];
  76. subtitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  77. NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:[_subtitleOptions.fontFamily getWithDefaultValue:nil] fontSize:[_subtitleOptions.fontSize getWithDefaultValue:@(12)] fontWeight:[_subtitleOptions.fontWeight getWithDefaultValue:nil] color:[_subtitleOptions.color getWithDefaultValue:nil]];
  78. [subtitleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.subtitle attributes:fontAttributes]];
  79. CGSize labelSize = [subtitleLabel.text sizeWithAttributes:fontAttributes];
  80. CGRect labelframe = subtitleLabel.frame;
  81. labelframe.size = labelSize;
  82. subtitleLabel.frame = labelframe;
  83. [subtitleLabel sizeToFit];
  84. if (_subtitleOptions.color.hasValue) {
  85. UIColor *color = _subtitleOptions.color.get;
  86. subtitleLabel.textColor = color;
  87. }
  88. [self.titleView addSubview:subtitleLabel];
  89. return subtitleLabel;
  90. }
  91. -(UILabel*)setupTitle {
  92. CGRect titleFrame = self.titleView.frame;
  93. if (self.subtitle) {
  94. titleFrame.size.height /= 2;
  95. }
  96. UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleFrame];
  97. titleLabel.textAlignment = NSTextAlignmentCenter;
  98. titleLabel.backgroundColor = [UIColor clearColor];
  99. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  100. NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:[_titleOptions.fontFamily getWithDefaultValue:nil] fontSize:[_titleOptions.fontSize getWithDefaultValue:@(16)] fontWeight:[_titleOptions.fontWeight getWithDefaultValue:nil] color:[_subtitleOptions.color getWithDefaultValue:nil]];
  101. [titleLabel setAttributedText:[[NSAttributedString alloc] initWithString:self.title attributes:fontAttributes]];
  102. CGSize labelSize = [titleLabel.text sizeWithAttributes:fontAttributes];
  103. CGRect labelframe = titleLabel.frame;
  104. labelframe.size = labelSize;
  105. titleLabel.frame = labelframe;
  106. [titleLabel sizeToFit];
  107. if (!self.subtitle) {
  108. titleLabel.center = self.titleView.center;
  109. }
  110. if (_titleOptions.color.hasValue) {
  111. UIColor *color = _titleOptions.color.get;
  112. titleLabel.textColor = color;
  113. }
  114. [self.titleView addSubview:titleLabel];
  115. return titleLabel;
  116. }
  117. @end