如何将椭圆形图层绘制到UITextField

时间:2019-04-11 09:38:18

标签: ios objective-c xcode

我需要在我的文本框上绘制椭圆形,如果有人帮助我绘制此设计阴影,我不知道如何使用图层进行绘制,

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码作为示例:

let shadowLayer = CAShapeLayer()
// set your shadow color here
shadowLayer.fillColor = UIColor.lightGray.cgColor 
shadowLayer.lineWidth = 0
// calc size of your shadow according to your design
let shadowSize = CGSize(width: textField.bounds.width + 40, height: 20)
let shadowBounds = CGRect(origin: .zero, size: shadowSize)
shadowLayer.path = UIBezierPath(ovalIn: shadowBounds).cgPath
shadowLayer.bounds = shadowBounds
shadowLayer.position = CGPoint(x: textField.bounds.midX, y: textField.bounds.maxY)
containerView.layer.insertSublayer(shadowLayer, at: 0)

在这里,我假设您有一些containerView,其中包含您的文本字段作为子视图,并在名为textField的变量中保留对该文本字段的引用。

答案 1 :(得分:1)

这是安东答案的Objective-C翻译,因为问题是针对Objective-C而不是Swift的:

CAShapeLayer *shadowLayer = [[CAShapeLayer alloc] init];
shadowLayer.fillColor = [UIColor lightGrayColor].CGColor;
shadowLayer.lineWidth = 0.0f;
CGSize shadowSize = CGSizeMake(textField.bounds.size.width + 40, 40);
CGRect shawdowBounds = CGRectMake(0, 0, shadowSize.width, shadowSize.height);
shadowLayer.path = [UIBezierPath bezierPathWithOvalInRect:shawdowBounds].CGPath;
shadowLayer.bounds = shawdowBounds;
shadowLayer.position = CGPointMake(CGRectGetMidX(textField.bounds), CGRectGetMaxY(textField.bounds));
[containerView.layer insertSublayer:shadowLayer atIndex0];