YBIBTopView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // YBIBTopView.m
  3. // YBImageBrowserDemo
  4. //
  5. // Created by 波儿菜 on 2019/7/6.
  6. // Copyright © 2019 杨波. All rights reserved.
  7. //
  8. #import "YBIBTopView.h"
  9. #import "YBIBIconManager.h"
  10. #import "YBIBUtilities.h"
  11. @interface YBIBTopView ()
  12. @property (nonatomic, strong) UILabel *pageLabel;
  13. @property (nonatomic, strong) UIButton *operationButton;
  14. @end
  15. @implementation YBIBTopView
  16. #pragma mark - life cycle
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. [self addSubview:self.pageLabel];
  21. [self addSubview:self.operationButton];
  22. // self.operationButton.backgroundColor = UIColor.redColor;
  23. [self.operationButton setHidden:true];
  24. [self setOperationType:YBIBTopViewOperationTypeMore];
  25. }
  26. return self;
  27. }
  28. - (void)layoutSubviews {
  29. [super layoutSubviews];
  30. CGFloat height = self.bounds.size.height, width = self.bounds.size.width;
  31. self.pageLabel.frame = CGRectMake((width - (width / 3)) / 2.0f , 0, width / 3, height);
  32. CGFloat buttonWidth = 54;
  33. self.operationButton.frame = CGRectMake(width - buttonWidth, 0, buttonWidth, height);
  34. }
  35. #pragma mark - public
  36. + (CGFloat)defaultHeight {
  37. return 50;
  38. }
  39. - (void)setPage:(NSInteger)page totalPage:(NSInteger)totalPage {
  40. if (totalPage <= 1) {
  41. self.pageLabel.hidden = YES;
  42. } else {
  43. self.pageLabel.hidden = NO;
  44. NSString *text = [NSString stringWithFormat:@"%ld/%ld", page + (NSInteger)1, totalPage];
  45. NSShadow *shadow = [NSShadow new];
  46. shadow.shadowBlurRadius = 4;
  47. shadow.shadowOffset = CGSizeMake(0, 1);
  48. shadow.shadowColor = UIColor.darkGrayColor;
  49. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSShadowAttributeName:shadow}];
  50. self.pageLabel.attributedText = attr;
  51. }
  52. }
  53. #pragma mark - event
  54. - (void)clickOperationButton:(UIButton *)button {
  55. if (self.clickOperation) self.clickOperation(self.operationType);
  56. }
  57. #pragma mark - getters & setters
  58. - (void)setOperationType:(YBIBTopViewOperationType)operationType {
  59. _operationType = operationType;
  60. UIImage *image = nil;
  61. switch (operationType) {
  62. case YBIBTopViewOperationTypeSave:
  63. image = [YBIBIconManager sharedManager].toolSaveImage();
  64. break;
  65. case YBIBTopViewOperationTypeMore:
  66. image = [YBIBIconManager sharedManager].toolMoreImage();
  67. break;
  68. }
  69. [self.operationButton setImage:image forState:UIControlStateNormal];
  70. }
  71. - (UILabel *)pageLabel {
  72. if (!_pageLabel) {
  73. _pageLabel = [UILabel new];
  74. _pageLabel.textColor = [UIColor whiteColor];
  75. _pageLabel.font = [UIFont boldSystemFontOfSize:16];
  76. _pageLabel.textAlignment = NSTextAlignmentCenter;
  77. _pageLabel.adjustsFontSizeToFitWidth = YES;
  78. }
  79. return _pageLabel;
  80. }
  81. - (UIButton *)operationButton {
  82. if (!_operationButton) {
  83. _operationButton = [UIButton buttonWithType:UIButtonTypeCustom];
  84. _operationButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
  85. _operationButton.titleLabel.adjustsFontSizeToFitWidth = YES;
  86. [_operationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  87. [_operationButton addTarget:self action:@selector(clickOperationButton:) forControlEvents:UIControlEventTouchUpInside];
  88. _operationButton.layer.shadowColor = UIColor.darkGrayColor.CGColor;
  89. _operationButton.layer.shadowOffset = CGSizeMake(0, 1);
  90. _operationButton.layer.shadowOpacity = 1;
  91. _operationButton.layer.shadowRadius = 4;
  92. }
  93. return _operationButton;
  94. }
  95. @end