Quartz based drop shadows don't work for me on

2019-06-25 13:36发布

问题:

I have been able to add shadows to many UI objects in my view, including rather nicely drop shadowing some custom drawing in a UIImageView. However the same code produces nothing when applied to a UITextView:

[desc.layer setShadowColor:[[UIColor blackColor] CGColor]];
[desc.layer setShadowRadius:2.0f];
[desc.layer setShadowOffset:CGSizeMake(1, 1)];
[desc.layer setShadowOpacity:0.8f];

Is this something I'm missing? I was hoping to use quartz to add a border and a drop shadow. The border code does work when commented in:

//Border.
/*
[desc.layer setBackgroundColor:[[KookaSettings sharedInstance].cellBackgroundColorD CGColor]];
[desc.layer setBorderColor: [[UIColor blackColor] CGColor]];
[desc.layer setBorderWidth:1.0f];
[desc.layer setCornerRadius:8.0f];
*/

回答1:

I know that the drop shadow doesn't work if the view has clipsToBounds set. Perhaps UITextView has that set by default?



回答2:

You need to set clipsToBounds to NO for the shadow, but the downside is that if your text is longer than the visible area of the view and you need to scroll it, then it will no longer be clipped to the view's visible boundaries. I'm looking for a clean way around this.



回答3:

Guillaume is right about the clipsToBounds setting.

It's a little messy, but you can always add a clear view behind the UITextView and drop a shadow on it...