酷店

XLPageTitleCell.m 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // XLPageTitleCell.m
  3. // XLPageViewControllerExample
  4. //
  5. // Created by MengXianLiang on 2019/5/13.
  6. // Copyright © 2019 jwzt. All rights reserved.
  7. // https://github.com/mengxianliang/XLPageViewController
  8. #import <UIKit/UIKit.h>
  9. #import "XLPageTitleCell.h"
  10. #pragma mark -
  11. #pragma mark 自定义cell类
  12. @interface XLPageTitleLabel : UILabel
  13. @property (nonatomic, assign) XLPageTextVerticalAlignment textVerticalAlignment;
  14. @end
  15. @implementation XLPageTitleLabel
  16. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
  17. CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
  18. switch (self.textVerticalAlignment) {
  19. case XLPageTextVerticalAlignmentCenter:
  20. textRect.origin.y = (bounds.size.height - textRect.size.height)/2.0;
  21. break;
  22. case XLPageTextVerticalAlignmentTop: {
  23. CGFloat topY = self.font.pointSize > [UIFont labelFontSize] ? 0 : 2;
  24. textRect.origin.y = topY;
  25. }
  26. break;
  27. case XLPageTextVerticalAlignmentBottom:
  28. textRect.origin.y = bounds.size.height - textRect.size.height;
  29. break;
  30. default:
  31. break;
  32. }
  33. return textRect;
  34. }
  35. - (void)drawTextInRect:(CGRect)requestedRect {
  36. CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
  37. [super drawTextInRect:actualRect];
  38. }
  39. @end
  40. #pragma mark -
  41. #pragma mark Cell类
  42. @implementation XLPageTitleCell
  43. - (instancetype)initWithFrame:(CGRect)frame {
  44. if (self = [super initWithFrame:frame]) {
  45. self.textLabel = [[XLPageTitleLabel alloc] init];
  46. self.textLabel.textAlignment = NSTextAlignmentCenter;
  47. [self.contentView addSubview:self.textLabel];
  48. self.config = [XLPageViewControllerConfig defaultConfig];
  49. }
  50. return self;
  51. }
  52. - (void)layoutSubviews {
  53. [super layoutSubviews];
  54. self.textLabel.frame = self.bounds;
  55. }
  56. - (void)configCellOfSelected:(BOOL)selected {
  57. self.textLabel.textColor = selected ? self.config.titleSelectedColor : self.config.titleNormalColor;
  58. self.textLabel.font = selected ? self.config.titleSelectedFont : self.config.titleNormalFont;
  59. XLPageTitleLabel *label = (XLPageTitleLabel *)self.textLabel;
  60. label.textVerticalAlignment = self.config.textVerticalAlignment;
  61. }
  62. - (void)showAnimationOfProgress:(CGFloat)progress type:(XLPageTitleCellAnimationType)type {
  63. if (type == XLPageTitleCellAnimationTypeSelected) {
  64. self.textLabel.textColor = [XLPageViewControllerUtil colorTransformFrom:self.config.titleSelectedColor to:self.config.titleNormalColor progress:progress];
  65. }else if (type == XLPageTitleCellAnimationTypeWillSelected){
  66. self.textLabel.textColor = [XLPageViewControllerUtil colorTransformFrom:self.config.titleNormalColor to:self.config.titleSelectedColor progress:progress];
  67. }
  68. }
  69. @end