简体   繁体   English

UITextView内部阴影

[英]UITextView Inner Shadow

I'm trying to create an inner shadow on only the top portion of a UITextView , but I'm having some difficulty. 我正在尝试仅在UITextView的顶部创建内部阴影,但是遇到了一些困难。 I'm borrowing code from this post, but I don't understand exactly what's going on. 我从这篇文章中借用了代码,但是我不完全了解发生了什么。

CAShapeLayer* shadowLayer = [CAShapeLayer layer];
[shadowLayer setFrame:[self bounds]];

// Standard shadow stuff
[shadowLayer setShadowColor:[[UIColor colorWithWhite:0 alpha:1] CGColor]];
[shadowLayer setShadowOffset:CGSizeMake(0.0f, 0.0f)];
[shadowLayer setShadowOpacity:1.0f];
[shadowLayer setShadowRadius:5];

// Causes the inner region in this example to NOT be filled.
[shadowLayer setFillRule:kCAFillRuleEvenOdd];

// Create the larger rectangle path.
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectInset(bounds, -42, -42));

// Add the inner path so it's subtracted from the outer path.
// someInnerPath could be a simple bounds rect, or maybe
// a rounded one for some extra fanciness.
CGPathAddPath(path, NULL, someInnerPath);
CGPathCloseSubpath(path);

[shadowLayer setPath:path];
CGPathRelease(path);

[[self layer] addSublayer:shadowLayer];

What exactly is "someInnerPath" if I want a simple shadow at only the top of a rectangular UITextView (I don't need the rounded effect). 如果我只希望在矩形UITextView的顶部有一个简单的阴影(我不需要四舍五入的效果),那么“ someInnerPath”到底是什么。

Try this instead in your viewDidLoad or wherever fits you best: 在您的viewDidLoad或最适合您的位置尝试以下方法:

CGFloat inset = -20;

UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(inset, 0,
                                                                       self.textView.bounds.size.width - inset - inset,
                                                                       self.textView.bounds.size.height)];

UIView *overlayView = [[UIView alloc]initWithFrame:self.textView.frame];
overlayView.userInteractionEnabled = NO;
overlayView.clipsToBounds = YES;

CALayer *layer = overlayView.layer;
layer.shadowPath = shadowPath.CGPath;
layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0].CGColor;
layer.shadowOpacity = 1.0;
layer.shadowRadius = 10;
layer.shadowOffset = CGSizeMake(0, self.textView.bounds.size.height * -1);

[self.view addSubview:overlayView];

The inset is to make sure the shadow doesn't stop just before the corners. 插图是确保阴影不会在拐角处停止。 Try setting it to zero, and you'll see what I mean. 尝试将其设置为零,您会明白我的意思。

Basically what the code is doing is creating a view who's layer's shadow is offset to the top of the view. 基本上,代码正在执行的操作是创建一个视图,该视图的层的阴影偏移到该视图的顶部。 I tried adding the overlayView directly to the textView, but it would sort of stick to the scrolling, so when I scrolled down, a big black box appeared. 我尝试将overlayView直接添加到textView中,但是这会影响滚动,因此当我向下滚动时,会出现一个大黑框。 Adding the overlayView to self.view instead solved the problem. 相反,将overlayView添加到self.view可以解决此问题。

You may want to play around with the shadowColor, shadowOpacity and shadowRadius parameters to your liking. 您可能想根据自己的喜好使用shadowColor,shadowOpacity和shadowRadius参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM