Dinesh Raja Dinesh Raja - 4 years ago 142
iOS Question

Create shadow using QuartzCore for UITextView

I have created a shadow using

for my
with this following code.

myTextView.layer.masksToBounds = NO;
myTextView.layer.shadowColor = [UIColor blackColor].CGColor;
myTextView.layer.shadowOpacity = 0.7f;
myTextView.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
myTextView.layer.shadowRadius = 8.0f;
myTextView.layer.shouldRasterize = YES;

It creates a
looks good too.
Here it is my output for the above code.

enter image description here

But When I try to add a text to the
, my textView text goes out of the bounds and it looks outside of the
like below.

enter image description here

It's happening only when I add shadow. The text inside the
is not showing weird If I don't add shadow.What I am doing wrong?? How could I overcome this? Why it is happening?


I found It is happening, because of setting the
maskToBounds = NO;
If we set
then we cannot get shadow. Reason Here it is an answer

Answer Source

There is no "right" solution, because of UIView behavior. When masksToBounds is NO, any sublayers that extend outside the layer's boundaries will be visible. And UITextField scroll text outside the UITextField layer.

Add clear view behind the UITextView and drop a shadow on it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download