TCScrollTextView.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #import "TCScrollTextView.h"
  2. #import "NSAttributedString+Size.h"
  3. @interface TCScrollTextView ()
  4. @end
  5. @implementation TCScrollTextView
  6. - (void)dealloc {
  7. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
  8. }
  9. - (instancetype)initWithFrame:(CGRect)frame {
  10. if (self = [super initWithFrame:frame]) {
  11. [self initUI];
  12. }
  13. return self;
  14. }
  15. - (void)initUI {
  16. _contentLabel = [[UILabel alloc] init];
  17. _contentLabel.backgroundColor = [UIColor clearColor];
  18. _contentLabel.textColor = [UIColor whiteColor];
  19. [self addSubview:_contentLabel];
  20. //超出部分不显示
  21. self.layer.masksToBounds = YES;
  22. //设置默认值
  23. self.speed = 0.5;
  24. //解决进入后台然后回来就停了
  25. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:UIApplicationDidBecomeActiveNotification object:nil];
  26. }
  27. - (void)startAnimation {
  28. [self setNeedsLayout];
  29. }
  30. - (void)setText:(NSString *)text {
  31. _text = text;
  32. _contentLabel.text = text;
  33. //根据内容得到_contentLabel的frame
  34. [_contentLabel sizeToFit];
  35. //居中显示
  36. CGRect frame = _contentLabel.frame;
  37. frame.origin.y = (self.bounds.size.height - frame.size.height)/2;
  38. _contentLabel.frame = frame;
  39. }
  40. //NSAttributedString *abString = [_richText attributedString];
  41. //float richTextLableWidth = [abString getWidthWithHeight:25] + 20;
  42. //_richTextLab.rtFrame = CGRectMake(10, 5, richTextLableWidth, 20);
  43. - (void)setTextAttributedString:(NSAttributedString *)abString {
  44. //_text = text;
  45. //_contentLabel.text = text;
  46. _contentLabel.attributedText = abString;
  47. //根据内容得到_contentLabel的frame
  48. [_contentLabel sizeToFit];
  49. //居中显示
  50. CGRect frame = _contentLabel.frame;
  51. frame.origin.y = (self.bounds.size.height - frame.size.height)/2;
  52. _contentLabel.frame = frame;
  53. //float richTextLableWidth = [abString getWidthWithHeight:25] + 20;
  54. }
  55. - (void)setFont:(UIFont *)font {
  56. _font = font;
  57. _contentLabel.font = font;
  58. }
  59. - (void)setSpeed:(CGFloat)speed {
  60. _speed = speed;
  61. }
  62. - (void)layoutSubviews {
  63. [super layoutSubviews];
  64. CGFloat sizeWidth = CGRectGetWidth(_contentLabel.frame);
  65. [self.contentLabel.layer removeAnimationForKey:@"keyFrame"];
  66. //如果文字能够显示完全 则不用滚动显示
  67. if (sizeWidth<= self.bounds.size.width) {
  68. return;
  69. }
  70. //添加帧动画 实现滚动效果 其实就是改变x的值
  71. CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
  72. keyFrame.keyPath = @"transform.translation.x";
  73. keyFrame.values = @[@(0), @(-sizeWidth + self.bounds.size.width)];
  74. keyFrame.repeatCount = NSIntegerMax;
  75. keyFrame.autoreverses = NO;
  76. keyFrame.duration = self.speed * self.contentLabel.text.length*0.8;
  77. // keyFrame.duration = 12+(1/self.speed) * self.contentLabel.text.length*0.1;
  78. NSLog(@"layoutSubviews _speed:%f duration:%f lenth:%lu",_speed,keyFrame.duration,self.contentLabel.text.length);
  79. //滚动速度改变 可以实现滚动速度由慢变快或者由快变慢
  80. // keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithControlPoints:0 :0 :0.5 :0.5]];
  81. [self.contentLabel.layer addAnimation:keyFrame forKey:@"keyFrame"];
  82. }
  83. @end