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.
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 = ...
}