Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing with clear color on UIView (cutting a hole) in static method

I have an iPhone application and I need to implement the following method:

+(UITextView *)textView:(UITextView *) withCuttedRect:(CGRect)r

This method must cut (fill with [UIColor clearColor]) the rect r from UITextView and return UITextView object.

The user will see the view behind UITextView from the cutted holes.

How can it be done?

like image 627
user1385666 Avatar asked Jun 13 '12 11:06

user1385666


1 Answers

When you would have something like:

 +(UITextView *)textView:(UITextView *)textView withCuttedRect:(CGRect)r {
}

you actually can simply access the textview's layer from core animation with

 textView.layer

What you then can to is set a mask for clipping. These masks work the following way: you generally draw a black shape, and that stays the same, the rest will be clipped (ok, you actually can also do some things on the alpha channel but roughly that is it).

So you need a black rectangle as a mask, with a rectangle within the rectangle which is free. for that you can approximately do

 CAShapeLayer *mask = [[CAShapeLayer alloc] init];
 mask.frame = self.textView.layer.bounds;
 CGRect biggerRect = CGRectMake(mask.frame.origin.x, mask.frame.origin.y, mask.frame.size.width, mask.frame.size.height);
 CGRect smallerRect = CGRectMake(50.0f, 50.0f, 10.0f, 10.0f);

 UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath moveToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];

[maskPath moveToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];

 mask.path = maskPath.CGPath;
[mask setFillRule:kCAFillRuleEvenOdd];
 mask.fillColor = [[UIColor blackColor] CGColor];
 self.textView.layer.mask = mask;

The code above is also disused by Crop a CAShapeLayer retrieving the external path

The idea why that filling works that way is nicely explained in Quartz 2D Programming Guide in the section "Filling a Path"

like image 61
Joerg Simon Avatar answered Oct 22 '22 20:10

Joerg Simon