首页 > 代码库 > iOS学习 - 23 加载本地 txt文件,NSMutableParagraphStyle 段落格式,缩放动画

iOS学习 - 23 加载本地 txt文件,NSMutableParagraphStyle 段落格式,缩放动画

思路:

  1.new 一个 Empty 后缀为 .txt 文件,内容随笔拷贝一段

  2.用 NSString 接收本地文件,再用一个标题拼接字符串

  3.创建一个 NSMutableParagraphStyle 实例,设置标题居中、标题距离正文间隔

  4.创建一个 NSMutableParagraphStyle 实例,设置正文左对齐、每段开头缩进 10

  5.创建一个基础动画的实例,用代理监听动画结束

话不多说上代码

#pragma mark -- 拼接字符- (void)loadData:(NSString *)filename {        //第一个段落    NSMutableParagraphStyle *first = [[NSMutableParagraphStyle alloc]init];    first.alignment = NSTextAlignmentCenter;    first.lineSpacing = 10;        //第二个段落    NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];    //从左开始写    second.alignment = NSTextAlignmentLeft;    //首行缩进    second.firstLineHeadIndent = 10;    //间距    second.lineSpacing = 10;        UIFont *titleFont = [UIFont systemFontOfSize:16];    UIFont *contextFont = [UIFont systemFontOfSize:13];        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:@"游戏许可及服务协议" attributes:@{NSParagraphStyleAttributeName:first,NSFontAttributeName:titleFont}];        //标题拼接正文,正文前加换行符    NSString *str = [NSString stringWithFormat:@"\n%@",filename];        NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:str attributes:@{NSParagraphStyleAttributeName:second,NSFontAttributeName:contextFont}];        [str1 appendAttributedString:str2];    _textView.attributedText = str1;}#pragma mark -- 关闭动画- (void)closeAniamtion:(UIView *)view {    CABasicAnimation *animation = [CABasicAnimation animation];    animation.keyPath = @"transform.scale";    animation.toValue = @0.9;    animation.duration = 0.4;    animation.delegate = self;    animation.removedOnCompletion = NO;    animation.fillMode = kCAFillModeForwards;    [view.layer addAnimation:animation forKey:@"closeAnimation"];}#pragma mark -- 本地 txt 文件内容- (NSString *)filename {    //本地 txt 文件    NSString *path = [[NSBundle mainBundle]pathForResource:@"浅遇时光,静好无恙.txt" ofType:nil];    //取出内容    NSString *filename = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    return filename;}
#pragma mark -- 代理监听动画停止- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {    if ([_bgView.layer animationForKey:@"closeAnimation"] == anim) {        [_bgView removeFromSuperview];    }}

完整代码在 github 上,点我下载

iOS学习 - 23 加载本地 txt文件,NSMutableParagraphStyle 段落格式,缩放动画