harryisaac
harryisaac

Reputation: 1141

Wrapping Text in a UITextView Around a UIImage WITHOUT CoreText

Is there a way to wrap text from a UITextView around a UIImage without using CoreText?

I have been playing around with attributed strings without much luck, and CoreText just seems extremely complicated so I would rather stay out of it.

Upvotes: 37

Views: 11851

Answers (3)

Dannie P
Dannie P

Reputation: 4622

This seems to do the trick:

UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
self.textView.textContainer.exclusionPaths = @[imgRect];

Swift,

textView.textContainer.exclusionPaths = [UIBezierPath(...)]

or just

textView.textContainer.exclusionPaths = [UIBezierPath(rect: the image frame)]

Note that the path is expressed in the frame of the text view.

Hence for example ...

yourTextView.textContainer.exclusionPaths =
 [UIBezierPath(rect:
  CGRect(
   origin: CGPoint(x: yourTextView.frame.width - 50, y: 0),
   size: CGSize(width: 50, height: 50))
)]

that would leave out a rectangle, 50x50, at the top right of your text view.

Note too that in many cases you have to set the exclusion area in layout - since both your text view and the image may have been reshaped or shaped for the first time.

override func layoutSubviews() {
    super.layoutSubviews()
    ...
    yourTextView.textContainer.exclusionPaths = ...
}

Upvotes: 112

app_
app_

Reputation: 697

The short answer is you can't without CoreText pre iOS 7.

I've been struggling with this myself a while ago and this post was very helpful to me.

It is CoreText though.

Upvotes: 0

Callum
Callum

Reputation: 201

In Swift 4:

self.textView.textContainer.exclusionPaths = [UIBezierPath(rect: imageView.frame)]

Upvotes: 2

Related Questions