思路:

  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 = ; //第二个段落
NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];
//从左开始写
second.alignment = NSTextAlignmentLeft;
//首行缩进
second.firstLineHeadIndent = ;
//间距
second.lineSpacing = ; UIFont *titleFont = [UIFont systemFontOfSize:];
UIFont *contextFont = [UIFont systemFontOfSize:]; 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-); NSString *longSecondStr = [NSString stringWithFormat:@"\n%@",secondStr];
NSMutableAttributedString *secondAttr = [[NSMutableAttributedString alloc]initWithString:longSecondStr attributes:@{NSParagraphStyleAttributeName:second}];
NSRange range = {,b};
/** 添加间距属性 **/
[secondAttr addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:lineGap] range:range];
/** 拼接字符串 **/
[firstAttr appendAttributedString:secondAttr];
label.attributedText = firstAttr; }else if(b>a){ NSRange range = {,a};
/** 计算间距 **/
CGFloat lineGap = label.font.pointSize *(b-a)/(a-);
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];
}
}

完整代码在 github 上,传送门

iOS 学习 - 23 加载本地 txt 文件, NSMutableParagraphStyle 段落格式,缩放动画,字体间距的更多相关文章

  1. iOS开发-UIWebView加载本地和网络数据

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...

  2. iOS --- UIWebView的加载本地数据的三种方式

    UIWebView是IOS内置的浏览器,可以浏览网页,打开文档  html/htm  pdf   docx  txt等格式的文件.  safari浏览器就是通过UIWebView做的. 服务器将MIM ...

  3. Mysql加载本地CSV文件

    Mysql加载本地CSV文件 1.系统环境 系统版本:Win10 64位 Mysql版本: 8.0.15 MySQL Community Server - GPL Mysql Workbench版本: ...

  4. UIWebView加载本地html文件

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(, , KScreenWidth, KScreenHeight-)]; ...

  5. 360chrome,google chrome浏览器使用jquery.ajax加载本地html文件

    使用360chrome和google chrome浏览器加载本地html文件时,会报错,提示: XMLHttpRequest cannot load file:///Y:/jswg/code/html ...

  6. WebView加载本地Html文件并实现点击效果

    Webview是用来与前端交互的纽,可以加载本地Html文件,和网页并实现交互的功能. WebView通过WebSetting可以使用Android原生的JavascriptInterface来进行j ...

  7. @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件

    目录 @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件 运行 使用 配置 svg-sprite-loader 调用当前环境下的颜色 props @vue/cl ...

  8. ajax加载本地html文件出现 XMLHttpRequest cannot load的问题

    谷歌浏览器ajax加载本地html文件出现 XMLHttpRequest cannot load的问题(火狐中不会出现这问题) Cross origin requests are only suppo ...

  9. easyui 加载本地json 文件的方法

    easyui 加载本地json 文件的方法 2017年12月15日 17:18:07 vivian_hnd 阅读数 2155 https://blog.csdn.net/vivian_wang07/a ...

随机推荐

  1. Delete Volume 操作 - 每天5分钟玩转 OpenStack(57)

    今天讨论 cinder 如何删除 volume . 状态为 Available 的 volume 才能够被 delete.如果 volume 当前已经 attach 到 instance,需要先 de ...

  2. vc下的静态链接库与动态链接库(一)

    一.静态库与动态库的区别 目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Lib ...

  3. EntityFramework 7 smallint short 奇怪问题(已解决)

    在使用 EF7 进行条件查询的时候,遇到一个很奇怪的问题,不知道 EF 其他版本有没有这种情况,怎么说呢?一句话描述不清楚,具体请看下面内容. 问题场景 BloggingContext 配置代码: u ...

  4. Java多线程系列--“JUC锁”01之 框架

    本章,我们介绍锁的架构:后面的章节将会对它们逐个进行分析介绍.目录如下:01. Java多线程系列--“JUC锁”01之 框架02. Java多线程系列--“JUC锁”02之 互斥锁Reentrant ...

  5. iOS对象属性详解

    oc对象的一些属性: retain,strong, copy,weak,assign,readonly, readwrite, unsafe_unretained 下面来分别讲讲各自的作用和区别: r ...

  6. jquery自定义滚动条 鼠标移入或滚轮时显示 鼠标离开或悬停超时时隐藏

    一.需求: 我需要做一个多媒体播放页面,左侧为播放列表,右侧为播放器.为了避免系统滚动条把列表和播放器隔断开,左侧列表的滚动条需要自定义,并且滚动停止和鼠标离开时要隐藏掉. 二.他山之石: 案例来自h ...

  7. JavaMail基础案例开发

    Java邮件开发案例 所需jar包有:mailapi.jar,smtp.jar 一.单独邮件发送案例 package com.itRed.javamail; import java.util.Prop ...

  8. LeetCode - 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  9. 一步一步开发Game服务器(二)完成登陆,聊天

    我知道这样的文章在博客园已经多的大家都不想看了,但是这是我的系列文章开始,请各位大神见谅了. 多线程,线程执行器,(详见),socket通信相关 (详见) 本人blog相关文章测试代码,示例,完整版s ...

  10. IntelliJ IDEA 转移C盘.IntelliJIdea(索引目录)

    转移原因: C盘是机械硬盘,并且容量不多的情况下,建议转移. 转移步骤: 找到索引目录 win10系统下默认路径:C:\Users\asus\.IntelliJIdea2016.2 *复制或剪切到新的 ...