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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With