3

I have created a shadow using QuartzCore for my UITextView 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 shadow and 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 myTextView, my textView text goes out of the bounds and it looks outside of the myTextView like below.

enter image description here

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

UPDATE:

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

Community
  • 1
  • 1
Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
  • `masksToBounds` is set to `NO`. Without this, the view will not attempt to clip its content. – borrrden Nov 22 '12 at 06:00
  • If I set **myTextView.layer.masksToBounds = YES;** then it's not showing any shadow. – Dinesh Raja Nov 22 '12 at 06:04
  • Try setting `textView.contentInset` to an appropriate value so that it wont overlap there. – iDev Nov 22 '12 at 06:09
  • @ACB No. I think it's not anything to do with contentInset. Content Inset is only to make some space on all the sides. Anyway I tried as you said and it doesn't changed anything. – Dinesh Raja Nov 22 '12 at 06:17

3 Answers3

7

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.

AntonPalich
  • 1,488
  • 12
  • 13
  • What do you mean by clear View? What If I have another superview behind the textView?? – Dinesh Raja Nov 22 '12 at 06:20
  • I think what he meant is to add a separate UIVIew as a container view behind the textview and give shadow effect to that. If you are using this textview in multiple places, probably you can subclass a UIVIew and add textview as subview in the custom implementation. +1 for the suggestion. – iDev Nov 22 '12 at 06:25
  • @ACB But Actually I am trying to add a shadow for my textview. But you are giving alternate solution. I already know to add a UIView behind to textView to make a look like what i wanted. Anyway its answer my question **how could I overcome this?**. But why I am getting like my output? What If I want to add shadow only to textView? What If I have large number of textViews?? Then I need to create large number of UIViews unwantedly and making outlets for them too... – Dinesh Raja Nov 22 '12 at 06:34
  • +1 for suggestion to have an alternate way.. – Dinesh Raja Nov 22 '12 at 06:39
  • @R.A 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. – AntonPalich Nov 22 '12 at 07:59
  • This is a horrible truth. – Matt Becker Aug 09 '13 at 17:58
2

Just add another UIView under your textView and set it's layer to display shadow (do not forget to set it's background color to something other than clear color - or shadow wont be drawn)

myTextView = [UITextView alloc] initWithFrame:CGRectMake(100,100,200,200);
UIView* shadowView = [UIView alloc] initWithFrame:myTextView.frame];
shadowView.backgroundColor = myTextView.backgroundColor;
shadowView.layer.masksToBounds = NO;
shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
shadowView.layer.shadowOpacity = 0.7f;
shadowView.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
shadowView.layer.shadowRadius = 8.0f;
shadowView.layer.shouldRasterize = YES;
[someView addSubview:shadowView];
[someView addSubView:myTextView];
Oladya Kane
  • 877
  • 7
  • 9
1

In addition to the previous answer, just copy all attributes from the original uiTextView to the helper view:

UITextView *helperTextView = [[UITextView alloc] init];
helperTextView = textView;  //copy all attributes to the helperTextView;

shadowTextView = [[UIView alloc] initWithFrame:textView.frame];
shadowTextView.backgroundColor = textView.backgroundColor;
shadowTextView.layer.opacity = 1.0f;
shadowTextView.layer.masksToBounds = NO;
shadowTextView.layer.shadowColor = [UIColorFromRGB(0x00abff)CGColor];
shadowTextView.layer.shadowOpacity = 1.0f;
shadowTextView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
shadowTextView.layer.shadowRadius = 10.0f;
shadowTextView.layer.cornerRadius = 8.0f;
shadowTextView.layer.shouldRasterize = YES;

[self.view addSubview:shadowTextView];
[self.view addSubview:helperTextView];
Ing. Ron
  • 2,005
  • 2
  • 17
  • 33