LMJHorizontalScrollText.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //
  2. // LMJHorizontalScrollText.m
  3. // LMJHorizontalScrollTextExample
  4. //
  5. // Created by LiMingjie on 2019/8/22.
  6. // Copyright © 2019 LMJ. All rights reserved.
  7. //
  8. #import "LMJHorizontalScrollText.h"
  9. @interface LMJHorizontalScrollText()
  10. @property (nonatomic, strong) UILabel * contentLabel1;
  11. @property (nonatomic, strong) UILabel * contentLabel2;
  12. @property (nonatomic, assign) CGFloat textWidth;
  13. @property (nonatomic, assign) int wanderingOffset;
  14. @property (nonatomic, strong) NSTimer * timer;
  15. @end
  16. @implementation LMJHorizontalScrollText
  17. {
  18. CGFloat _selfWidth;
  19. CGFloat _selfHeight;
  20. }
  21. #pragma mark - Init
  22. - (id)init{
  23. self = [super init];
  24. if (self) {
  25. self.frame = CGRectMake(0, 0, 100, 20); // 设置一个初始的frame
  26. }
  27. return self;
  28. }
  29. - (id)initWithFrame:(CGRect)frame{
  30. self = [super initWithFrame:frame];
  31. if (self) {
  32. [self setInitialSettings];
  33. }
  34. return self;
  35. }
  36. - (void)awakeFromNib{
  37. [super awakeFromNib];
  38. [self setInitialSettings];
  39. }
  40. - (void)layoutSubviews{
  41. _selfWidth = self.frame.size.width;
  42. _selfHeight = self.frame.size.height;
  43. }
  44. - (void)setInitialSettings {
  45. _contentLabel1 = nil;
  46. _contentLabel2 = nil;
  47. _text = @"";
  48. _textFont = [UIFont systemFontOfSize:12];
  49. _textColor = [UIColor blackColor];
  50. _speed = 0.03;
  51. _textWidth = 0;
  52. _wanderingOffset = -1;
  53. _moveMode = LMJTextScrollWandering;
  54. _moveDirection = LMJTextScrollMoveLeft;
  55. _timer = nil;
  56. }
  57. #pragma mark - Set
  58. - (void)setText:(NSString *)text{
  59. if ([_text isEqualToString: text]) return;
  60. _text = text;
  61. [self updateTextWidth];
  62. [self updateLabelsFrame];
  63. }
  64. - (void)setTextFont:(UIFont *)textFont{
  65. if (_textFont == textFont) return;
  66. _textFont = textFont;
  67. [self updateTextWidth];
  68. [self updateLabelsFrame];
  69. }
  70. - (void)setTextColor:(UIColor *)textColor{
  71. _textColor = textColor;
  72. if (_contentLabel1 != nil) {
  73. _contentLabel1.textColor = _textColor;
  74. }
  75. if (_contentLabel2 != nil) {
  76. _contentLabel2.textColor = _textColor;
  77. }
  78. }
  79. - (void)setSpeed:(CGFloat)speed{
  80. _speed = speed;
  81. [self move];
  82. }
  83. - (void)setMoveMode:(LMJTextScrollMode)moveMode {
  84. _moveMode = moveMode;
  85. if (self.text.length == 0) {//如果字符串长度为0,直接返回
  86. return;
  87. }
  88. BOOL isMoving = NO;
  89. if (_timer) {
  90. [self stop];
  91. isMoving = YES;
  92. }
  93. [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  94. [obj removeFromSuperview];
  95. obj = nil;
  96. }];
  97. switch (_moveMode) {
  98. case LMJTextScrollContinuous:
  99. {
  100. if (self.moveDirection == LMJTextScrollMoveLeft) {
  101. [self creatLabel1WithFrame1:CGRectMake(0, 0, _textWidth, _selfHeight) andLabel2WithFrame2:CGRectMake(_textWidth, 0, _textWidth, _selfHeight)];
  102. }else{
  103. [self creatLabel1WithFrame1:CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight) andLabel2WithFrame2:CGRectMake(_selfWidth -_textWidth -_textWidth, 0, _textWidth, _selfHeight)];
  104. }
  105. }break;
  106. case LMJTextScrollIntermittent:
  107. {
  108. if (self.moveDirection == LMJTextScrollMoveLeft) {
  109. [self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, _selfHeight)];
  110. }else{
  111. [self creatLabel1WithFrame:CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight)];
  112. }
  113. }break;
  114. case LMJTextScrollFromOutside:
  115. {
  116. if (self.moveDirection == LMJTextScrollMoveLeft) {
  117. [self creatLabel1WithFrame:CGRectMake(_selfWidth, 0, _textWidth, _selfHeight)];
  118. }else{
  119. [self creatLabel1WithFrame:CGRectMake(_textWidth, 0, _textWidth, _selfHeight)];
  120. }
  121. }break;
  122. case LMJTextScrollWandering:
  123. {
  124. [self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, _selfHeight)];
  125. }break;
  126. default:
  127. break;
  128. }
  129. if (isMoving) {
  130. [self move];
  131. }
  132. }
  133. #pragma mark - Create Labels
  134. -(void)creatLabel1WithFrame:(CGRect)frame{
  135. _contentLabel1 = [[UILabel alloc] initWithFrame:frame];
  136. _contentLabel1.text = self.text;
  137. _contentLabel1.font = self.textFont;
  138. _contentLabel1.textColor = self.textColor;
  139. _contentLabel1.backgroundColor = [UIColor clearColor];
  140. [self addSubview:_contentLabel1];
  141. if (_contentLabel2) {
  142. [_contentLabel2 removeFromSuperview];
  143. _contentLabel2 = nil;
  144. }
  145. }
  146. -(void)creatLabel1WithFrame1:(CGRect)frame1 andLabel2WithFrame2:(CGRect)frame2{
  147. [self creatLabel1WithFrame:frame1];
  148. _contentLabel2 = [[UILabel alloc] initWithFrame:frame2];
  149. _contentLabel2.text = self.text;
  150. _contentLabel2.font = self.textFont;
  151. _contentLabel2.textColor = self.textColor;
  152. _contentLabel2.backgroundColor = [UIColor clearColor];
  153. [self addSubview:_contentLabel2];
  154. }
  155. #pragma mark - Util
  156. - (void)updateTextWidth {
  157. _textWidth = [self.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.textFont, NSFontAttributeName, nil]].width;
  158. }
  159. - (void)updateLabelsFrame {
  160. CGFloat label1_x = _contentLabel1.frame.origin.x;
  161. CGFloat label2_x = _contentLabel2.frame.origin.x;
  162. if (_contentLabel1 && _contentLabel2) {
  163. if (label1_x < label2_x) {
  164. _contentLabel1.frame = CGRectMake(label1_x, 0, _textWidth, _selfHeight);
  165. _contentLabel2.frame = CGRectMake(label1_x + _textWidth, 0, _textWidth, _selfHeight);
  166. } else {
  167. _contentLabel2.frame = CGRectMake(label2_x, 0, _textWidth, _selfHeight);
  168. _contentLabel1.frame = CGRectMake(label2_x + _textWidth, 0, _textWidth, _selfHeight);
  169. }
  170. } else {
  171. if (_contentLabel1) {
  172. _contentLabel1.frame = CGRectMake(label1_x, 0, _textWidth, _selfHeight);
  173. }
  174. if (_contentLabel2) {
  175. _contentLabel2.frame = CGRectMake(label2_x, 0, _textWidth, _selfHeight);
  176. }
  177. }
  178. }
  179. #pragma mark - Move Action
  180. // LMJTextScrollContinuous
  181. -(void)moveContinuous{
  182. if (self.moveDirection == LMJTextScrollMoveLeft) {
  183. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x -1, 0, _textWidth, _selfHeight);
  184. _contentLabel2.frame = CGRectMake(_contentLabel2.frame.origin.x -1, 0, _textWidth, _selfHeight);
  185. if (_contentLabel1.frame.origin.x < -_textWidth) {
  186. _contentLabel1.frame = CGRectMake(_contentLabel2.frame.origin.x + _textWidth, 0, _textWidth, _selfHeight);
  187. }
  188. if (_contentLabel2.frame.origin.x < -_textWidth) {
  189. _contentLabel2.frame = CGRectMake(_contentLabel1.frame.origin.x + _textWidth, 0, _textWidth, _selfHeight);
  190. }
  191. }else{
  192. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x +1, 0, _textWidth, _selfHeight);
  193. _contentLabel2.frame = CGRectMake(_contentLabel2.frame.origin.x +1, 0, _textWidth, _selfHeight);
  194. if (_contentLabel1.frame.origin.x > _selfWidth) {
  195. _contentLabel1.frame = CGRectMake(_contentLabel2.frame.origin.x - _textWidth, 0, _textWidth, _selfHeight);
  196. }
  197. if (_contentLabel2.frame.origin.x > _selfWidth) {
  198. _contentLabel2.frame = CGRectMake(_contentLabel1.frame.origin.x - _textWidth, 0, _textWidth, _selfHeight);
  199. }
  200. }
  201. }
  202. // LMJTextScrollIntermittent
  203. -(void)moveIntermittent{
  204. if (self.moveDirection == LMJTextScrollMoveLeft) {
  205. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x -1, 0, _textWidth, _selfHeight);
  206. if (_contentLabel1.frame.origin.x < -_textWidth) {
  207. _contentLabel1.frame = CGRectMake(0, 0, _textWidth, _selfHeight);
  208. }
  209. }else{
  210. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x +1, 0, _textWidth, _selfHeight);
  211. if (_contentLabel1.frame.origin.x > _selfWidth) {
  212. _contentLabel1.frame = CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight);
  213. }
  214. }
  215. }
  216. // LMJTextScrollFromOutside
  217. -(void)moveFromOutside{
  218. if (self.moveDirection == LMJTextScrollMoveLeft) {
  219. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x -1, 0, _textWidth, _selfHeight);
  220. if (_contentLabel1.frame.origin.x < -_textWidth) {
  221. _contentLabel1.frame = CGRectMake(_selfWidth, 0, _textWidth, _selfHeight);
  222. }
  223. }else{
  224. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x +1, 0, _textWidth, _selfHeight);
  225. if (_contentLabel1.frame.origin.x > _selfWidth) {
  226. _contentLabel1.frame = CGRectMake(-_textWidth, 0, _textWidth, _selfHeight);
  227. }
  228. }
  229. }
  230. // LMJTextScrollWandering
  231. -(void)moveWandering{
  232. if (_textWidth > _selfWidth) {
  233. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x + _wanderingOffset, 0, _textWidth, _selfHeight);
  234. if (_contentLabel1.frame.origin.x < -(_textWidth -_selfWidth +2)) {
  235. _wanderingOffset = 1;
  236. }
  237. if (_contentLabel1.frame.origin.x > 2) {
  238. _wanderingOffset = -1;
  239. }
  240. }else if (_textWidth < _selfWidth){
  241. _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x + _wanderingOffset, 0, _textWidth, _selfHeight);
  242. if (_contentLabel1.frame.origin.x < 0) {
  243. _wanderingOffset = 1;
  244. }
  245. if (_contentLabel1.frame.origin.x > _selfWidth - _textWidth) {
  246. _wanderingOffset = -1;
  247. }
  248. }
  249. }
  250. #pragma mark - API Methods
  251. - (void)move {
  252. if (_timer != nil) {
  253. [self stop];
  254. }
  255. switch (self.moveMode) {
  256. case LMJTextScrollContinuous:
  257. {
  258. _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveContinuous) userInfo:nil repeats:YES];
  259. }
  260. break;
  261. case LMJTextScrollIntermittent:
  262. {
  263. _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveIntermittent) userInfo:nil repeats:YES];
  264. }
  265. break;
  266. case LMJTextScrollFromOutside:
  267. {
  268. _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveFromOutside) userInfo:nil repeats:YES];
  269. }
  270. break;
  271. case LMJTextScrollWandering:
  272. {
  273. _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveWandering) userInfo:nil repeats:YES];
  274. }
  275. break;
  276. default:
  277. break;
  278. }
  279. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  280. }
  281. - (void)stop {
  282. [_timer invalidate];
  283. _timer = nil;
  284. }
  285. - (void)dealloc {
  286. [self stop];
  287. }
  288. @end