【问题标题】:Adding inner shadow on uitextfield and uitextview在 uitextfield 和 uitextview 上添加内阴影
【发布时间】:2014-01-09 12:39:42
【问题描述】:

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

我在 viewcontroller 类中调用以下方法来在我的文本字段上添加阴影。

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

}

【问题讨论】:

    标签: ios objective-c uitextfield uitextview


    【解决方案1】:

    这个

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

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

    【讨论】:

      【解决方案2】:

      使用 UIBezierPath

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

      然后将它与图层一起使用。

      【讨论】:

        猜你喜欢
        • 2011-02-03
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多