Add Inner Shadow in UITextField(Any View)

You should rewrite  – (void)drawRect:(CGRect)rect   method.

 

And put this inside

 

 

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

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*