[Swift通天遁地]八、媒体与动画-(14)使用TextKit快速实现文章的分栏效果

本文将演示对长文本进行分栏显示。往项目中导入一份文本文件。

在左侧的项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在开始编写代码,加载文本文件中的内容,并对文字进行分栏。

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8         
 9         //文本视图控件提供了很大的灵活性,可以快速进行富文本视图的创建和设计。
10         //在此初始化一个指定显示区域的文本视图,用来实现杂志中常见的分栏效果。
11         let firstTextView = UITextView(frame: CGRect(x: 20, y: 40, width: 135, height: 200))
12         //设置第一个视图的背景颜色为棕色。
13         firstTextView.backgroundColor = UIColor.brown
14         //取消文本视图的可滚动性,使多余的无法显示的文字,自动进入第二个文本视图。
15         firstTextView.isScrollEnabled = false;
16         //将第一个文本视图添加到根视图。
17         self.view.addSubview(firstTextView)
18         
19         //获得第一个视图的文字存储属性
20         let textStorage = firstTextView.textStorage
21         //创建一个字符串,表示文本文件在项目中的路径。
22         let path = Bundle.main.url(forResource: "word", withExtension: "txt")
23         //添加一个异常捕捉语句,用来读取文本文件。
24         do
25         {
26             //读取文本文件中的文字内容。
27             let string = try String(contentsOf: path!)
28             //将读取的文字,赋予文本视图的文字存储属性。
29             textStorage.replaceCharacters(in: NSRange(location: 0,length: 0), with: string)
30         }
31         catch
32         {
33             print("Something went wrong :(")
34         }
35         
36         //创建第二个文本视图的显示区域,它和第一个文本视图的尺寸相同,但是位于第一个文本视图的右侧。
37         let secondRect = CGRect(x: 165, y: 40, width: 135, height: 200)
38         //初始化一个文字容器对象。
39         let secondTextContainer = NSTextContainer()
40         //使用文字容器对象,创建第二个文本视图。
41         let secondTextView = UITextView(frame: secondRect, textContainer: secondTextContainer)
42         //设置第二个文本视图的背景颜色为棕色
43         secondTextView.backgroundColor = UIColor.brown
44         //取消文本视图的可滚动性
45         secondTextView.isScrollEnabled = false
46         //将第二个文本视图添加到根视图。
47         self.view.addSubview(secondTextView)
48         
49         //使用相同的方式,创建最后一个文本视图,该视图位于两个文本视图的下方。
50         let thirdRect = CGRect(x: 20, y: 250, width: 280, height: 300)
51         //初始化一个文字容器对象。
52         let thirdTextContainer = NSTextContainer()
53         //使用文字容器对象,创建第三个文本视图。
54         let thirdTextView = UITextView(frame: thirdRect, textContainer: thirdTextContainer)
55         //设置第三个文本视图的背景颜色为紫色。
56         thirdTextView.backgroundColor = UIColor.purple
57         //取消第三个文本视图的可滚动性。
58         thirdTextView.isScrollEnabled = false
59         //将第三个文本视图添加到根视图
60         self.view.addSubview(thirdTextView)
61         
62         //初始化一个布局管理器。
63         let layoutManager = NSLayoutManager()
64         //将第一个文本视图的文字容器,添加到布局管理器。
65         layoutManager.addTextContainer(firstTextView.textContainer)
66         //将第二个文本视图的文字容器,也添加到布局管理器。
67         layoutManager.addTextContainer(secondTextContainer)
68         //将第三个文本视图的文字容器,添加到布局管理器,
69         //管理器就可以将三个文本视图视为一个容器,
70         //当内容超出第一个文本视图的显示范围时,将自动填充第二个文本视图,以此类推。
71         layoutManager.addTextContainer(thirdTextContainer)
72         //将布局管理器,赋予一个文本视图的文字存储属性,
73         //从而使三个文本视图,都可以显示第一个文本视图的文字存储属性的内容。
74         textStorage.addLayoutManager(layoutManager)
75     }
76 
77     override func didReceiveMemoryWarning() {
78         super.didReceiveMemoryWarning()
79         // Dispose of any resources that can be recreated.
80     }
81 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10354901.html