Create shadow using QuartzCore for UITextView [duplicate]

雨燕双飞 提交于 2019-12-03 17:17:58

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.

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];

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!