vladasha
vladasha

Reputation: 403

Break text that covers other views

Is it possible to break the text automatically, so the text isn't covering it, like the example below, in UITextview?

Image

Upvotes: 3

Views: 91

Answers (3)

Janmenjaya
Janmenjaya

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 :

enter image description here

Hope it helps

Happy coding ...

Upvotes: 0

Manishankar
Manishankar

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

androidExplorer
androidExplorer

Reputation: 136

You should use NSLineBreakByCharWrapping meaning that the word'll be cut after a character.

textview.textContainer.lineBreakMode = NSLineBreakByCharWrapping;

Upvotes: 0

Related Questions