Wrapping Text in a UITextView Around a UIImage WITHOUT CoreText
Asked Answered
U

3

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.

Unblown answered 4/11, 2012 at 4:45 Comment(0)
A
112

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(...the image frame)]

Works only from iOS 7 and up.

Anjanetteanjela answered 17/11, 2013 at 17:22 Comment(6)
@JoeBlow yes, just a normal UITextView. Thanks )Anjanetteanjela
How to add image in that rect areaAsthmatic
@deepakkumar I'd probably try adding your image view on top of UITextView at the needed locationAnjanetteanjela
Its possible on multiple imagesDwarfism
I would vote this as the single best iOS answer on StackOverflow!Garvey
well explained in this article:littlebitesofcocoa.com/…Fatness
C
2

In Swift 4:

self.textView.textContainer.exclusionPaths = [UIBezierPath(rect: imageView.frame)]
Coronet answered 27/7, 2020 at 17:48 Comment(3)
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?Arratoon
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).Coronet
This is what I'm trying #66026571Arratoon
A
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.

Ardyce answered 4/11, 2012 at 6:45 Comment(1)
Ok, thanks. I guess I'll just have to figure out this CoreText stuff. Thanks.Unblown

© 2022 - 2024 — McMap. All rights reserved.