在uitextfield和uitextview上添加内部阴影

时间:2014-01-09 12:39:42

标签: ios objective-c uitextfield uitextview

我正在尝试在少数UITextField和UITextView上添加内部阴影。我已将这些控件的cornerRadius属性设置为1.0。我正在使用下面提到的代码来创建内部阴影。但问题是阴影出现时带有陡峭的矩形角,而文本视图和文本字段则带有圆角。

我在viewcontroller类中调用follow方法在我的文本字段中添加阴影。

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

}

2 个答案:

答案 0 :(得分:0)

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

会产生这个。你可以操纵它来获得你想要的效果。

Text Field in code

答案 1 :(得分:0)

使用 UIBezierPath

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

而不是与Layer一起使用。