CCZTrotingLabel.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // CCZTrotingLabel.m
  3. // CCZTrotView
  4. //
  5. // Created by 金峰 on 16/9/25.
  6. // Copyright © 2016年 金峰. All rights reserved.
  7. //
  8. #import "CCZTrotingLabel.h"
  9. @interface CCZTrotingLabel ()
  10. @property (nonatomic, strong) NSMutableArray *attributeArr;
  11. @property (nonatomic, assign) CGFloat normalRate;
  12. @end
  13. @implementation CCZTrotingLabel
  14. - (NSMutableArray *)attributeArr {
  15. if (!_attributeArr) {
  16. _attributeArr = [NSMutableArray arrayWithCapacity:1];
  17. }
  18. return _attributeArr;
  19. }
  20. - (instancetype)init {
  21. self = [super init];
  22. if (self) {
  23. [self basicSetting];
  24. }
  25. return self;
  26. }
  27. - (void)basicSetting {
  28. self.rate = CCZTrotingRateNormal;
  29. __weak typeof(self) weakSelf = self;
  30. [self trotingStop:^{
  31. if (weakSelf.attributeArr.count) {
  32. [weakSelf.attributeArr removeObject:weakSelf.attributeArr.firstObject];
  33. if (weakSelf.attributeArr.count != 0) {
  34. [weakSelf addTrotAttribute:nil];
  35. }
  36. }
  37. }];
  38. }
  39. - (void)addTrotAttribute:(CCZTrotingAttribute *)attribute {
  40. if (attribute) {
  41. [self.attributeArr addObject:attribute];
  42. }
  43. CCZTrotingAttribute *trotingAtt = self.attributeArr.firstObject;
  44. if (!self.isTroting && _currentLabel) {
  45. [self trotingWithAttribute:trotingAtt];
  46. [self updateTroting];
  47. }
  48. if (!_currentLabel) {
  49. _currentLabel = [[UILabel alloc] init];
  50. _currentLabel.font = [UIFont systemFontOfSize:fitScreenWidth(12)];
  51. _currentLabel.textColor = [UIColor colorWithString:@"#FF8523"];
  52. [self trotingWithAttribute:trotingAtt];
  53. [self addTrotView:_currentLabel];
  54. }
  55. }
  56. - (void)addText:(NSString *)text {
  57. CCZTrotingAttribute *trotingAtt = [[CCZTrotingAttribute alloc] init];
  58. trotingAtt.text = text;
  59. [self addTrotAttribute:trotingAtt];
  60. }
  61. - (void)trotingWithAttribute:(CCZTrotingAttribute *)att {
  62. _currentLabel.text = att.text;
  63. if (att.attribute) {
  64. _currentLabel.attributedText = att.attribute;
  65. }
  66. CGSize textSize = [att.text sizeWithAttributes:@{NSFontAttributeName: _currentLabel.font}];
  67. _currentLabel.frame = CGRectMake(0, 0, textSize.width, textSize.height);
  68. __weak typeof(self) weakSelf = self;
  69. [self troting:^{
  70. CGSize trotContrinerSize = weakSelf.trotContaierView.frame.size;
  71. if (weakSelf.direction == CCZTrotDirectionLeft || weakSelf.direction == CCZTrotDirectionRight) {
  72. weakSelf.duration = (trotContrinerSize.width + textSize.width) / _normalRate;
  73. } else {
  74. weakSelf.duration = (trotContrinerSize.height + textSize.height) / _normalRate;
  75. }
  76. }];
  77. }
  78. - (void)setRate:(CCZTrotingRate)rate {
  79. _rate = rate;
  80. if (rate == CCZTrotingRateNormal) {
  81. _normalRate = 40;
  82. } else if(rate == CCZTrotingRateFast) {
  83. _normalRate = 90;
  84. } else {
  85. _normalRate = 10;
  86. }
  87. }
  88. @end