gpt4 book ai didi

ios - UITextView 内阴影

转载 作者:行者123 更新时间:2023-11-29 12:45:45 24 4
gpt4 key购买 nike

我试图只在 UITextView 的顶部创建一个内部阴影,但我遇到了一些困难。我从 this 借用代码发布,但我不明白到底发生了什么。

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

如果我只希望在矩形 UITextView 的顶部有一个简单的阴影(我不需要圆角效果),“someInnerPath”到底是什么。

最佳答案

在你的 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];

插图是为了确保阴影不会恰好在拐角处停止。尝试将其设置为零,您就会明白我的意思。

基本上代码所做的是创建一个 View ,该 View 的图层阴影偏移到 View 的顶部。我尝试将 overlayView 直接添加到 textView,但它会有点粘在滚动上,所以当我向下滚动时,会出现一个大黑框。将 overlayView 添加到 self.view 反而解决了问题。

您可能想根据自己的喜好使用 shadowColor、shadowOpacity 和 shadowRadius 参数。

关于ios - UITextView 内阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23724986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com