37

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.

yeesterbunny
  • 1,847
  • 2
  • 13
  • 17
harryisaac
  • 1,121
  • 1
  • 10
  • 18

3 Answers3

112

This seems to do the trick:

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

Works only from iOS 7 and up.

app_
  • 697
  • 5
  • 12
Dannie P
  • 4,464
  • 3
  • 29
  • 48
2

In Swift 4:

self.textView.textContainer.exclusionPaths = [UIBezierPath(rect: imageView.frame)]
Callum
  • 141
  • 8
  • I kind need to add a UIView(), I'm trying `let view = UIView()` and `[UIBezierPath(rect: view.frame)]`, got no error but still not working... any idea? – Pablo Feb 02 '21 at 18:02
  • hmm.. has the view's frame been defined by the time this is being called? if using auto layout, you ma need to add this to `override func viewDidLayoutSubviews()` (if in `UIViewControlelr`) or `override func layoutSubviews()` (if in `UIView`). – Callum Feb 03 '21 at 20:06
  • This is what I'm trying https://stackoverflow.com/questions/66026071/how-can-i-add-uiview-to-uitextview?noredirect=1#comment116740501_66026071 – Pablo Feb 04 '21 at 10:17
0

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.

shim
  • 9,289
  • 12
  • 69
  • 108
app_
  • 697
  • 5
  • 12