Agregar sombra interna en uitextfield y uitextview

Estoy tratando de agregar sombra interna en pocos UITextField y UITextView. He establecido la propiedad cornerRadius para estos controles a 1.0. y estoy usando el código que se menciona a continuación para crear la sombra interna. Pero el problema es que la sombra aparece con las esquinas empinadas rectangulares, mientras que la vista de text y el campo de text tienen esquinas networkingondeadas.

Estoy llamando al método siguiente en mi class viewcontroller para agregar la sombra en mi campo de text.

[self addInnerShadow:myTextField.frame]; - (void)addInnerShadow:(CGRect)frame { CAShapeLayer* shadowLayer = [CAShapeLayer layer]; [shadowLayer setFrame:frame]; [shadowLayer setShadowColor:[[UIColor blackColor] CGColor]]; [shadowLayer setShadowOffset:CGSizeMake(0.0f, 0.0f)]; [shadowLayer setShadowOpacity:1.0f]; [shadowLayer setShadowRadius:5]; [shadowLayer setFillRule:kCAFillRuleEvenOdd]; CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, NULL, CGRectInset(CGRectMake(0, 0, frame.size.width, frame.size.height),-5, -5)); CGMutablePathRef someInnerPath = CGPathCreateMutable(); CGPathAddRect(someInnerPath, NULL, CGRectInset(CGRectMake(0, 0, frame.size.width, frame.size.height), 0, 0)); CGPathAddPath(path, NULL, someInnerPath); CGPathCloseSubpath(path); [shadowLayer setPath:path]; CGPathRelease(path); CAShapeLayer* maskLayer = [CAShapeLayer layer]; [maskLayer setPath:someInnerPath]; [shadowLayer setMask:maskLayer]; [[self.view layer] addSublayer:shadowLayer]; } 

Esta

 CGContextRef context = UIGraphicsGetCurrentContext(); // Shadow UIColor* shadow4 = [[UIColor blackColor] colorWithAlphaComponent: 0.81]; CGSize shadow4Offset = CGSizeMake(0.1, 2.1); CGFloat shadow4BlurRadius = 5; UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(20.5, 26.5, 200, 40) cornerRadius: 4]; [[UIColor whiteColor] setFill]; [rectanglePath fill]; CGRect rectangleBorderRect = CGRectInset([rectanglePath bounds], -shadow4BlurRadius, - shadow4BlurRadius); rectangleBorderRect = CGRectOffset(rectangleBorderRect, -shadow4Offset.width, - shadow4Offset.height); rectangleBorderRect = CGRectInset(CGRectUnion(rectangleBorderRect, [rectanglePath bounds]), -1, -1); UIBezierPath* rectangleNegativePath = [UIBezierPath bezierPathWithRect: rectangleBorderRect]; [rectangleNegativePath appendPath: rectanglePath]; rectangleNegativePath.usesEvenOddFillRule = YES; CGContextSaveGState(context); { CGFloat xOffset = shadow4Offset.width + round(rectangleBorderRect.size.width); CGFloat yOffset = shadow4Offset.height; CGContextSetShadowWithColor(context, CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)), shadow4BlurRadius, shadow4.CGColor); [rectanglePath addClip]; CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rectangleBorderRect.size.width), 0); [rectangleNegativePath applyTransform: transform]; [[UIColor grayColor] setFill]; [rectangleNegativePath fill]; } CGContextRestoreGState(context); [[UIColor blackColor] setStroke]; rectanglePath.lineWidth = 0.5; [rectanglePath stroke]; 

Producirá esto. Puedes manipular esto para get el efecto deseado.

Campo de texto en código

Crear ruta con UIBezierPath

 UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5.0, 5.0)]; 

Y que usarlo con Layer.