FMLrcView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // FMLrcView.m
  3. // FreeMusic
  4. //
  5. //
  6. #import "FMLrcView.h"
  7. #import "UIView+Additions.h"
  8. @implementation FMLrcView
  9. - (id)initWithFrame:(CGRect)frame
  10. {
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. keyArr = [NSMutableArray new];
  14. titleArr = [NSMutableArray new];
  15. labels = [NSMutableArray new];
  16. _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  17. _scrollView.userInteractionEnabled = NO;
  18. [self addSubview:_scrollView];
  19. }
  20. return self;
  21. }
  22. -(void)setLrcSourcePath:(NSString *)path
  23. {
  24. if ([keyArr count]!=0) {
  25. [keyArr removeAllObjects];
  26. }
  27. if ([titleArr count]!=0) {
  28. [titleArr removeAllObjects];
  29. }
  30. NSString * string =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  31. NSArray *array = [string componentsSeparatedByString:@"\n"];
  32. for (NSString * str in array) {
  33. if (!str || str.length <= 0)
  34. continue;
  35. [self parseLrcLine:str];
  36. }
  37. [self bubbleSort:keyArr];
  38. _scrollView.contentOffset = CGPointMake(0, 0);
  39. _scrollView.contentSize = CGSizeMake(_scrollView.size.width, [keyArr count]*25);
  40. [self addLabelForScrollView];
  41. }
  42. -(float)getNumberWith:(NSString *)string
  43. {
  44. NSArray * array = [string componentsSeparatedByString:@":"];
  45. NSString * str = [NSString stringWithFormat:@"%@.%@",[array objectAtIndex:0],[array objectAtIndex:1]];
  46. return [str floatValue];
  47. }
  48. - (void)bubbleSort:(NSMutableArray *)array
  49. {
  50. int i, y;
  51. BOOL bFinish = YES;
  52. for (i = 1; i<= [array count] && bFinish; i++) {
  53. bFinish = NO;
  54. for (y = (int)[array count]-1; y>=i; y--) {
  55. float num1 = [self getNumberWith:[array objectAtIndex:y]];
  56. float num2 = [self getNumberWith:[array objectAtIndex:y-1]];
  57. if (num1 < num2) {
  58. [array exchangeObjectAtIndex:y-1 withObjectAtIndex:y];
  59. [titleArr exchangeObjectAtIndex:y-1 withObjectAtIndex:y];
  60. bFinish = YES;
  61. }
  62. }
  63. }
  64. }
  65. -(void)selfClearKeyAndTitle
  66. {
  67. if ([keyArr count]!=0) {
  68. [keyArr removeAllObjects];
  69. }
  70. if ([titleArr count]!=0) {
  71. [titleArr removeAllObjects];
  72. }
  73. }
  74. -(void)scrollViewClearSubView
  75. {
  76. for (UIView * sub in _scrollView.subviews) {
  77. [sub removeFromSuperview];
  78. }
  79. if ([labels count]!=0) {
  80. [labels removeAllObjects];
  81. }
  82. }
  83. -(void)addLabelForScrollView
  84. {
  85. [self scrollViewClearSubView];
  86. for (int index = 0; index<[titleArr count]; index++) {
  87. NSString * title = [titleArr objectAtIndex:index];
  88. UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 25*index+(_scrollView.size.height/2), _scrollView.size.width, 25)];
  89. label.text = title;
  90. label.textColor = [UIColor lightGrayColor];
  91. label.font = [UIFont systemFontOfSize:14.0f];
  92. label.textAlignment = NSTextAlignmentCenter;
  93. [_scrollView addSubview:label];
  94. [labels addObject:label];
  95. }
  96. }
  97. -(void)scrollViewMoveLabelWith:(NSString *)string
  98. {
  99. if ([keyArr count]!=0) {
  100. int index = 0;
  101. BOOL isFinded = NO;
  102. for (; index<[keyArr count]; index++) {
  103. NSString * key = [keyArr objectAtIndex:index];
  104. float num1 = [self getNumberWith:key];
  105. float num2 = [self getNumberWith:string];
  106. if (num1 == num2) {
  107. isFinded = YES;
  108. break;
  109. }
  110. }
  111. if (isFinded) {
  112. UILabel * label = [labels objectAtIndex:index];
  113. label.textColor = [UIColor colorWithRed:192.0f/255.0f green:37.0f/255.0f blue:62.0f/255.0f alpha:1.0f];
  114. [_scrollView setContentOffset:CGPointMake(0, 25*index) animated:YES];
  115. }
  116. }
  117. }
  118. -(NSString*) parseLrcLine:(NSString *)sourceLineText
  119. {
  120. if (!sourceLineText || sourceLineText.length <= 0)
  121. return nil;
  122. NSArray *array = [sourceLineText componentsSeparatedByString:@"\n"];
  123. for (int i = 0; i < array.count; i++) {
  124. NSString *tempStr = [array objectAtIndex:i];
  125. NSArray *lineArray = [tempStr componentsSeparatedByString:@"]"];
  126. for (int j = 0; j < [lineArray count]-1; j ++) {
  127. if ([lineArray[j] length] > 8) {
  128. NSString *str1 = [tempStr substringWithRange:NSMakeRange(3, 1)];
  129. NSString *str2 = [tempStr substringWithRange:NSMakeRange(6, 1)];
  130. if ([str1 isEqualToString:@":"] && [str2 isEqualToString:@"."]) {
  131. NSString *lrcStr = [lineArray lastObject];
  132. NSString *timeStr = [[lineArray objectAtIndex:j] substringWithRange:NSMakeRange(1, 8)];//分割区间求歌词时间
  133. //把时间 和 歌词 加入词典
  134. [keyArr addObject:[timeStr substringToIndex:5]];
  135. [titleArr addObject:lrcStr];
  136. }
  137. }
  138. }
  139. }
  140. return nil;
  141. }
  142. /*
  143. // Only override drawRect: if you perform custom drawing.
  144. // An empty implementation adversely affects performance during animation.
  145. - (void)drawRect:(CGRect)rect
  146. {
  147. // Drawing code
  148. }
  149. */
  150. @end