思路:

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

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

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

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

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

话不多说上代码

  1. #pragma mark -- 拼接字符
  2. - (void)loadData:(NSString *)filename {
  3.  
  4. //第一个段落
  5. NSMutableParagraphStyle *first = [[NSMutableParagraphStyle alloc]init];
  6. first.alignment = NSTextAlignmentCenter;
  7. first.lineSpacing = ;
  8.  
  9. //第二个段落
  10. NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];
  11. //从左开始写
  12. second.alignment = NSTextAlignmentLeft;
  13. //首行缩进
  14. second.firstLineHeadIndent = ;
  15. //间距
  16. second.lineSpacing = ;
  17.  
  18. UIFont *titleFont = [UIFont systemFontOfSize:];
  19. UIFont *contextFont = [UIFont systemFontOfSize:];
  20.  
  21. NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:@"游戏许可及服务协议" attributes:@{NSParagraphStyleAttributeName:first,NSFontAttributeName:titleFont}];
  22.  
  23. //标题拼接正文,正文前加换行符
  24. NSString *str = [NSString stringWithFormat:@"\n%@",filename];
  25.  
  26. NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:str attributes:@{NSParagraphStyleAttributeName:second,NSFontAttributeName:contextFont}];
  27.  
  28. [str1 appendAttributedString:str2];
  29. _textView.attributedText = str1;
  30. }
  31.  
  32. #pragma mark -- 关闭动画
  33. - (void)closeAniamtion:(UIView *)view {
  34. CABasicAnimation *animation = [CABasicAnimation animation];
  35. animation.keyPath = @"transform.scale";
  36. animation.toValue = @0.9;
  37. animation.duration = 0.4;
  38. animation.delegate = self;
  39. animation.removedOnCompletion = NO;
  40. animation.fillMode = kCAFillModeForwards;
  41. [view.layer addAnimation:animation forKey:@"closeAnimation"];
  42. }
  43.  
  44. #pragma mark -- 本地 txt 文件内容
  45. - (NSString *)filename {
  46. //本地 txt 文件
  47. NSString *path = [[NSBundle mainBundle]pathForResource:@"浅遇时光,静好无恙.txt" ofType:nil];
  48. //取出内容
  49. NSString *filename = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  50. return filename;
  51. }
  1. #pragma mark -- 代理监听动画停止
  2. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
  3.  
  4. if ([_bgView.layer animationForKey:@"closeAnimation"] == anim) {
  5. [_bgView removeFromSuperview];
  6. }
  7. }

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

  1. - (void)baseChinese:(UILabel *)label loadFirst:(NSString *)firstStr second:(NSString *)secondStr
  2. {
  3. //第一个段落
  4. NSMutableParagraphStyle *first = [[NSMutableParagraphStyle alloc]init];
  5. //第二个段落
  6. NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];
  7.  
  8. int a = (int)firstStr.length;
  9. int b = (int)secondStr.length;
  10.  
  11. if (a>b) {
  12.  
  13. NSDictionary *dict1 = @{NSParagraphStyleAttributeName:first};
  14. NSMutableAttributedString *firstAttr = [[NSMutableAttributedString alloc]initWithString:firstStr attributes:dict1];
  15. /** 计算间距 **/
  16. CGFloat lineGap = label.font.pointSize *(a-b)/(b-);
  17.  
  18. NSString *longSecondStr = [NSString stringWithFormat:@"\n%@",secondStr];
  19. NSMutableAttributedString *secondAttr = [[NSMutableAttributedString alloc]initWithString:longSecondStr attributes:@{NSParagraphStyleAttributeName:second}];
  20. NSRange range = {,b};
  21. /** 添加间距属性 **/
  22. [secondAttr addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:lineGap] range:range];
  23. /** 拼接字符串 **/
  24. [firstAttr appendAttributedString:secondAttr];
  25. label.attributedText = firstAttr;
  26.  
  27. }else if(b>a){
  28.  
  29. NSRange range = {,a};
  30. /** 计算间距 **/
  31. CGFloat lineGap = label.font.pointSize *(b-a)/(a-);
  32. NSDictionary *dict1 = @{NSParagraphStyleAttributeName:first};
  33. NSMutableAttributedString *firstAttr = [[NSMutableAttributedString alloc]initWithString:firstStr attributes:dict1];
  34. /** 添加间距属性 **/
  35. [firstAttr addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:lineGap] range:range];
  36.  
  37. NSString *longSecondStr = [NSString stringWithFormat:@"\n%@",secondStr];
  38. NSMutableAttributedString *secondAttr = [[NSMutableAttributedString alloc]initWithString:longSecondStr attributes:@{NSParagraphStyleAttributeName:second}];
  39. /** 拼接字符串 **/
  40. [firstAttr appendAttributedString:secondAttr];
  41. label.attributedText = firstAttr;
  42.  
  43. }else{
  44. /** 如果相等,直接拼接 **/
  45. label.text = [NSString stringWithFormat:@"%@\n%@",firstStr,secondStr];
  46. }
  47. }

完整代码在 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. Backup Volume 操作 - 每天5分钟玩转 OpenStack(59)

    本节我们讨论 volume 的 Backup 操作. Backup 是将 volume 备份到别的地方(备份设备),将来可以通过 restore 操作恢复. Backup VS Snapshot 初看 ...

  2. Android随笔之——闹钟制作铺垫之AlarmManager详解

    说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...

  3. 用PHP抓取页面并分析

    在做抓取前,记得把php.ini中的max_execution_time设置的大点,不然会报错的.

  4. 使用GIT@OSChina 实现协同工作的方法。

    由于我新建了一个团队,团队里的人对于GIT都不太熟悉,所以才有了这篇文章.我用的是git-1.9.4的版本,所以我建议团队里面的成员也使用这个版本.首先是下载git,这个自己去网上找吧,一大堆,记得是 ...

  5. 20 个看起来很棒的 Web UI 工具包

    程序员们比设计师更需要这些 UI 方面的内容: 1. Mini Reminders Mini Reminders 2. Transluscent UI elements Transluscent UI ...

  6. 五一干货来袭!开源Moon.Orm标准版发布!

    标准版源代码下载: 链接:http://pan.baidu.com/s/1i3xj0f7 因五一早过(现在中旬了),解压码获取请到: http://www.cnblogs.com/humble/p/3 ...

  7. C#测试题若干,都是基础阿

    类的以下特性中,可以用于方便地重用已有的代码和数据的是( ).   A.多态B.封装C.继承D.抽象 答案:http://hovertree.com/tiku/bjaf/a3k6pgq5.htm 可用 ...

  8. 基于Quartz.NET构建自己的动态作业调度器

    在日常的开发中,运行定时任务基本上已经是很普遍的需求了,可以通过windows服务+timer组件来实现,也可以使用第三方框架来集成,Quartz.NET就是一款从JAVA的Quartz移植过来的一个 ...

  9. iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)

    iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器  实例代码 - (void)viewDidLoad { [super viewDidLoad]; / ...

  10. WPF筛选、排序和分组

    可以通过CollectionViewSource或者CollectionView对视图进行排序.筛选和分组. 一.通过CollectionViewSource listingDataView是Coll ...