Reputation: 403
Is it possible to break the text automatically, so the text isn't covering it, like the example below, in UITextview?
Upvotes: 3
Views: 91
Reputation: 4163
You can do exactly the design like this way. Although I have not used Swift 3, but you can format it accordingly to the syntax.
Here the exclusionPath
will be responsible for starting the text after image and exclusionPath1
to start the text below image, as per your requirement.
My Code :
let txtView = UITextView(frame: CGRectMake(0, 64, SCREEN_SIZE.width, 150));
self.view.addSubview(txtView);
let imageView = UIImageView(frame: CGRectMake(0, 0, 50, 50));
imageView.image = UIImage(named: "icon");
let exclusionPath = UIBezierPath(rect: CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height));
let exclusionPath1 = UIBezierPath(rect: CGRectMake(-50, 50, imageView.frame.size.width, imageView.frame.size.height));
txtView.textContainer.exclusionPaths = [exclusionPath, exclusionPath1];
txtView.addSubview(imageView);
Output :
Hope it helps
Happy coding ...
Upvotes: 0
Reputation: 327
Add CoreText
Framework
import CoreText
Add the below code after adding the imageView
to UITextView
,
var exclusionPath = UIBezierPath(rect: CGRect(x: imageView.frame.orgin.x, y: imageView.frame.orgin.y, width: imageView.frame.size.width, height: imageView.frame.size.height))
textView.textContainer.exclusionPaths = [exclusionPath]
textView.addSubview(imageView)
Upvotes: 3
Reputation: 136
You should use NSLineBreakByCharWrapping
meaning that the word'll be cut after a character.
textview.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
Upvotes: 0