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

时间:2023-02-01 08:43:22

思路:

  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]; } }

 

字体间距,中文的文字宽度是相等的,英文和数字的宽度不一样

- (void)baseChinese:(UILabel *)label loadFirst:(NSString *)firstStr second:(NSString *)secondStr
{
    //第一个段落
    NSMutableParagraphStyle *first = [[NSMutableParagraphStyle alloc]init];
    //第二个段落
    NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];
    
    int a = (int)firstStr.length;
    int b = (int)secondStr.length;
    
    if (a>b) {

        NSDictionary *dict1 = @{NSParagraphStyleAttributeName:first};
        NSMutableAttributedString *firstAttr = [[NSMutableAttributedString alloc]initWithString:firstStr attributes:dict1];
        /** 计算间距 **/
        CGFloat lineGap = label.font.pointSize *(a-b)/(b-1);
        
        NSString *longSecondStr = [NSString stringWithFormat:@"\n%@",secondStr];
        NSMutableAttributedString *secondAttr = [[NSMutableAttributedString alloc]initWithString:longSecondStr attributes:@{NSParagraphStyleAttributeName:second}];
        NSRange range = {0,b};
        /** 添加间距属性 **/
        [secondAttr addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:lineGap] range:range];
        /** 拼接字符串 **/
        [firstAttr appendAttributedString:secondAttr];
        label.attributedText = firstAttr;
        
    }else if(b>a){
        
        NSRange range = {0,a};
        /** 计算间距 **/
        CGFloat lineGap = label.font.pointSize *(b-a)/(a-1);
        NSDictionary *dict1 = @{NSParagraphStyleAttributeName:first};
        NSMutableAttributedString *firstAttr = [[NSMutableAttributedString alloc]initWithString:firstStr attributes:dict1];
        /** 添加间距属性 **/
        [firstAttr addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:lineGap] range:range];
        
        NSString *longSecondStr = [NSString stringWithFormat:@"\n%@",secondStr];
        NSMutableAttributedString *secondAttr = [[NSMutableAttributedString alloc]initWithString:longSecondStr attributes:@{NSParagraphStyleAttributeName:second}];
        /** 拼接字符串 **/
        [firstAttr appendAttributedString:secondAttr];
        label.attributedText = firstAttr;
        
    }else{
        /** 如果相等,直接拼接 **/
        label.text = [NSString stringWithFormat:@"%@\n%@",firstStr,secondStr];
    }
}

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

 

完整代码在 github 上,传送门