react-native-navigation的迁移库

RNNTitleViewHelper.m 4.7KB

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