IOS 使用Text Kit做排版

1 let firstTextView = UITextView(frame:CGRect(x:20,
y:40, width:135, height:200))
2 firstTextView.backgroundColor = UIColor.brown
3 firstTextView.isScrollEnabled = false;
4 self.view.addSubview(firstTextView)
5 let textStorage = firstTextView.textStorage
6 let path = Bundle.main.url(forResource:“word”,
withExtension:“txt”)
7 do {
8 let string = try String(contentsOf:path!)
9 textStorage.replaceCharacters(in:NSRange(location:
0,length:0), with:string)
10 }
11 catch{
12 print(“读取文件错误!”)
13 }
14 let secondRect = CGRect(x:165, y:40, width:135,
height:200)
15 let secondTextContainer = NSTextContainer()
16 let secondTextView = UITextView(frame:secondRect,
textContainer:secondTextContainer)
17 secondTextView.backgroundColor = UIColor.brown
18 secondTextView.isScrollEnabled = false;
19 self.view.addSubview(secondTextView)
20 let thirdRect = CGRect(x:20, y:250, width:280,
height:300)
21 let thirdTextContainer = NSTextContainer()
22 let thirdTextView = UITextView(frame:thirdRect,
textContainer:thirdTextContainer)
23 thirdTextView.backgroundColor = UIColor.purple
24 thirdTextView.isScrollEnabled = false;
25 self.view.addSubview(thirdTextView)
26 let layoutManager = NSLayoutManager()
27
layoutManager.addTextContainer(firstTextView.textContainer)
28 layoutManager.addTextContainer(secondTextContainer)
29 layoutManager.addTextContainer(thirdTextContainer)
30 textStorage.addLayoutManager(layoutManager)

你可能感兴趣的:(IOS 使用Text Kit做排版)