Katedral Pillon
Katedral Pillon

Reputation: 14864

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

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:

Upvotes: 0

Views: 1146

Answers (2)

Kim
Kim

Reputation: 1462

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];

Upvotes: 0

matt
matt

Reputation: 535989

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.)

Upvotes: 0

Related Questions