IOS 使用Text Kit编排图文

1 let textView = UITextView(frame:CGRect(x:20, y:
40, width:280, height:500))
2 textView.backgroundColor = UIColor.brown
3 self.view.addSubview(textView)
4
5 let textStorage = textView.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 image = UIImage(named:“Tea”)
15 let imageView = UIImageView(image:image)
16 let rect = CGRect(x:80, y:80, width:150, height:

17 imageView.frame = rect
18
19 imageView.layer.cornerRadius = 75
20 imageView.layer.masksToBounds = true
21 imageView.layer.borderWidth = 10
22 self.view.addSubview(imageView)
23 var frame = textView.convert(imageView.bounds,
from:imageView)
24 frame.origin.x -= textView.textContainerInset.left;
25 frame.origin.y -= textView.textContainerInset.top;
26
27 let path2 = UIBezierPath(ovalIn:frame)
28 textView.textContainer.exclusionPaths = [path2]

你可能感兴趣的:(IOS 使用Text Kit编排图文)