How to apply custom bezier path to a UITextView inside a UITableViewCell

1.1k Views Asked by At

I am working with a UITableViewCell. The cell contains a number of images, a label, and a UITextView. I want the UITextView drawn with the other views as its exclusion paths. So inside my class MyUITableViewCell, I have the following code inside the method awakeFromNib.

UIBezierPath *image =[UIBezierPath bezierPathWithRect:self.animalImageView.bounds];
UIBezierPath *label =[UIBezierPath bezierPathWithRect:self.animalTitleView.bounds];
UIBezierPath *rating =[UIBezierPath bezierPathWithRect:self.ratingOfAnimal.bounds];

self.animalBioTextView.bounds=self.bounds;//resize textview to fill table cell; exclusions will come later
self.animalBioTextView.textContainer.exclusionPaths=@[image, label, rating];//apply exclusions
NSLog(@"Bezier path set");

None of this editing seem to have taken effect.

BWT: I declared the TableViewCell in the storyboard right inside the UITableView. Then I link the storyboard cell to MyUITableViewCell, and went from there. To clarify: everything works fine; except now that I am to customize the textContainer.

--UPDATE--

I thought my code sample show what I want to do, still here are some more explanations:

  • I want the textview to be contiguous with the TableViewCell on all sides. Since I could not accomplish this in the storyboard, I add the following line in the code: self.animalBioTextView.bounds=self.bounds

  • since the textview is contiguous with the cell, then I should be able to "easily" use the bounds of the child views and pass them to the textView. But evidently the bounds are with respect to the children themselves. So how do I get the coordinates with respect to the UITableViewCell? which in my case is self

2

There are 2 best solutions below

2
On

For one thing, you've neglected to think about coordinate systems. The bounds of the other views are each with respect to themselves; for example, their bounds origins are all (0,0). But the exclusion paths must be set with respect to the text container's own bounds origin (as projected into real-life space); it is very unlikely that the other views' origins are all at the exact top left of the text view's text container! So you need to convert all those rects to the text container's own coordinate system.

(Also, keep in mind that if the text view is scrollable, things are even more complicated, because the position of the other views in relation to the scrolled text content will change as the user scrolls, so you'll have to take that into account in your calculations and change the exclusion paths again.)

0
On

You can use convertRect:fromView: to get the coordinates relative to the text view. E.g.:

CGRect animalImageRect = [self.animalBioTextView convertRect:self.animalImageView.bounds fromView:self.animalImageView];
UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:animalImageRect];