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 = ;
- //第二个段落
- 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 段落格式,缩放动画,字体间距的更多相关文章
- iOS开发-UIWebView加载本地和网络数据
UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...
- iOS --- UIWebView的加载本地数据的三种方式
UIWebView是IOS内置的浏览器,可以浏览网页,打开文档 html/htm pdf docx txt等格式的文件. safari浏览器就是通过UIWebView做的. 服务器将MIM ...
- Mysql加载本地CSV文件
Mysql加载本地CSV文件 1.系统环境 系统版本:Win10 64位 Mysql版本: 8.0.15 MySQL Community Server - GPL Mysql Workbench版本: ...
- UIWebView加载本地html文件
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(, , KScreenWidth, KScreenHeight-)]; ...
- 360chrome,google chrome浏览器使用jquery.ajax加载本地html文件
使用360chrome和google chrome浏览器加载本地html文件时,会报错,提示: XMLHttpRequest cannot load file:///Y:/jswg/code/html ...
- WebView加载本地Html文件并实现点击效果
Webview是用来与前端交互的纽,可以加载本地Html文件,和网页并实现交互的功能. WebView通过WebSetting可以使用Android原生的JavascriptInterface来进行j ...
- @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件
目录 @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件 运行 使用 配置 svg-sprite-loader 调用当前环境下的颜色 props @vue/cl ...
- ajax加载本地html文件出现 XMLHttpRequest cannot load的问题
谷歌浏览器ajax加载本地html文件出现 XMLHttpRequest cannot load的问题(火狐中不会出现这问题) Cross origin requests are only suppo ...
- easyui 加载本地json 文件的方法
easyui 加载本地json 文件的方法 2017年12月15日 17:18:07 vivian_hnd 阅读数 2155 https://blog.csdn.net/vivian_wang07/a ...
随机推荐
- Backup Volume 操作 - 每天5分钟玩转 OpenStack(59)
本节我们讨论 volume 的 Backup 操作. Backup 是将 volume 备份到别的地方(备份设备),将来可以通过 restore 操作恢复. Backup VS Snapshot 初看 ...
- Android随笔之——闹钟制作铺垫之AlarmManager详解
说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
- 用PHP抓取页面并分析
在做抓取前,记得把php.ini中的max_execution_time设置的大点,不然会报错的.
- 使用GIT@OSChina 实现协同工作的方法。
由于我新建了一个团队,团队里的人对于GIT都不太熟悉,所以才有了这篇文章.我用的是git-1.9.4的版本,所以我建议团队里面的成员也使用这个版本.首先是下载git,这个自己去网上找吧,一大堆,记得是 ...
- 20 个看起来很棒的 Web UI 工具包
程序员们比设计师更需要这些 UI 方面的内容: 1. Mini Reminders Mini Reminders 2. Transluscent UI elements Transluscent UI ...
- 五一干货来袭!开源Moon.Orm标准版发布!
标准版源代码下载: 链接:http://pan.baidu.com/s/1i3xj0f7 因五一早过(现在中旬了),解压码获取请到: http://www.cnblogs.com/humble/p/3 ...
- C#测试题若干,都是基础阿
类的以下特性中,可以用于方便地重用已有的代码和数据的是( ). A.多态B.封装C.继承D.抽象 答案:http://hovertree.com/tiku/bjaf/a3k6pgq5.htm 可用 ...
- 基于Quartz.NET构建自己的动态作业调度器
在日常的开发中,运行定时任务基本上已经是很普遍的需求了,可以通过windows服务+timer组件来实现,也可以使用第三方框架来集成,Quartz.NET就是一款从JAVA的Quartz移植过来的一个 ...
- iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)
iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器 实例代码 - (void)viewDidLoad { [super viewDidLoad]; / ...
- WPF筛选、排序和分组
可以通过CollectionViewSource或者CollectionView对视图进行排序.筛选和分组. 一.通过CollectionViewSource listingDataView是Coll ...