对歌词的cell进一步封装,字体字号的设置都封装在自定义的cell中
QQMusicLrcCell.h
#import
@interface QQMusicLrcCell : UITableViewCell
@end
QQMusicLrcCell.m
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
QQMusicLrcCell *cell = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//设置
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
同时,左滑会出现歌词的界面,要实现这个效果,需要在storyboard中添加一个UIScrollView,覆盖在当前的歌手图片上
QQMusicLrcScrollView.h
#import
@interface QQMusicLrcScrollView : UIScrollView
/** 歌曲名称*/
@property (nonatomic, copy) NSString *musicName;
@end
QQMusicLrcScrollView.m
#import "QQMusicLrcScrollView.h"
#import "Masonry.h"
#import "QQMusicLrcTool.h"
#import "QQMusicLrcCell.h"
#import "QQMusicLrc.h"
static NSString * const reuseID = @"lrc";
@interface QQMusicLrcScrollView ()
/** tableView*/
@property (nonatomic, weak) UITableView *tableView;
/** 歌词数据*/
@property (nonatomic, strong) NSArray *lrcLines;
@end
@implementation QQMusicLrcScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setupTableView];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setupTableView];
}
return self;
}
- (void)setupTableView
{
//添加表格
UITableView *tableView = [[UITableView alloc]init];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:tableView];
tableView.dataSource = self;
self.tableView = tableView;
self.tableView.rowHeight = 30;
}
- (void)layoutSubviews
{
[super layoutSubviews];
//添加约束
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//顶部约束
make.top.equalTo(self);
//底部约束
make.bottom.equalTo(self);
//左边约束
make.left.equalTo(self).offset(self.bounds.size.width);
//右边约束
make.right.equalTo(self);
//宽度约束
make.width.equalTo(self);
//高度约束
make.height.equalTo(self);
}];
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.bounds.size.height * 0.5, 0, self.tableView.bounds.size.height * 0.5, 0);
}
#pragma mark - 表格数据源 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.lrcLines.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
QQMusicLrcCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (!cell) {
cell = [[QQMusicLrcCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
}
QQMusicLrc *musicLrc = self.lrcLines[indexPath.row];
cell.textLabel.text = musicLrc.lrc;
return cell;
}
#pragma mark - 重写set方法
- (void)setMusicName:(NSString *)musicName
{
_musicName = musicName;
//解析歌词
self.lrcLines = [QQMusicLrcTool lrcToolWithLrcName:_musicName];
//刷新表格
[self.tableView reloadData];
}
@end
歌词处理的工具
QQMusicLrcTool.h
#import
@interface QQMusicLrcTool : NSObject
+ (NSArray *)lrcToolWithLrcName:(NSString *)lrcName;
@end
QQMusicLrcTool.m
#import "QQMusicLrcTool.h"
#import "QQMusicLrc.h"
@implementation QQMusicLrcTool
/*
[ti:]
[ar:]
[al:]
[00:00.89]传奇
*/
+ (NSArray *)lrcToolWithLrcName:(NSString *)lrcName
{
//文件路径
NSString *path = [[NSBundle mainBundle]pathForResource:lrcName ofType:nil];
//读取
NSString *lrclist = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [lrclist componentsSeparatedByString:@"\n"];
NSMutableArray *lrcModels = [NSMutableArray array];
for (NSString *str in array) {
//过滤部分无用的字符串
if ([str hasPrefix:@"[ti:"] || [str hasPrefix:@"[ar:"] || [str hasPrefix:@"[al:"] || ![str hasPrefix:@"["]) continue ;
else {
QQMusicLrc *musicLrc = [QQMusicLrc musicLrcWithLrcName:str];
[lrcModels addObject:musicLrc];
}
}
return lrcModels;
}
@end
封装的歌词模型
QQMusicLrc.h
#import
@interface QQMusicLrc : NSObject
/** 歌词*/
@property (nonatomic, copy) NSString *lrc;
/** 时长*/
@property (nonatomic, assign) NSTimeInterval time;
+ (instancetype)musicLrcWithLrcName:(NSString *)lrcName;
- (instancetype)initWithLrcName:(NSString *)lrcName;
@end
QQMusicLrc.m
#import "QQMusicLrc.h"
@implementation QQMusicLrc
+ (instancetype)musicLrcWithLrcName:(NSString *)lrcName
{
return [[self alloc]initWithLrcName:lrcName];
}
- (instancetype)initWithLrcName:(NSString *)lrcName
{
if (self = [super init]) {
NSArray *array = [lrcName componentsSeparatedByString:@"]"];
self.lrc = array[1];
NSString *timeString = [array[0] substringFromIndex:1];
self.time = [self timeIntervalWithTimeString:timeString];
}
return self;
}
#pragma mark - 私有方法 将时间字符串转换为NSTimeInteval直接返回
- (NSTimeInterval)timeIntervalWithTimeString:(NSString *)timeString
{
NSArray *array = [timeString componentsSeparatedByString:@":"];
NSInteger min = [array[0] integerValue];
NSInteger second = [[array[1] substringWithRange:NSMakeRange(0, 2)] integerValue];
NSInteger ms = [[array[1] substringWithRange:NSMakeRange(3, 2)] integerValue];
return (min * 60 + second + ms / 1000);
}
@end
附上一张效果图