Wednesday, June 5, 2013

UIBezierPath UIRectCornerBottomLeft example in Objective C (iOS).


UIBezierPath UIRectCornerBottomLeft

UIRectCorner
The corners of a rectangle.

enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0
};
typedef NSUInteger UIRectCorner;

Constants
UIRectCornerTopLeft
The top-left corner of the rectangle.
UIRectCornerTopRight
The top-right corner of the rectangle.
UIRectCornerBottomLeft
The bottom-left corner of the rectangle.
UIRectCornerBottomRight
The bottom-right corner of the rectangle.
UIRectCornerAllCorners
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerBottomLeft]
The specified constants reflect the corners of a rectangle that has not been modified by an affine transform and is drawn in the default coordinate system (where the origin is in the upper-left corner and positive values extend down and to the right).

UIBezierPath UIRectCornerBottomLeft example.
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: btnView.bounds
                                               byRoundingCorners:UIRectCornerBottomLeft
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = btnView.bounds;
maskLayer.path = maskPath.CGPath;
btnView.layer.mask = maskLayer;
[btnView setNeedsDisplay];

Example of [UIBezierPath UIRectCornerBottomLeft].
UIView *containerView = [[UIView alloc] initWithFrame:someFrame];

UIRectCorners corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
CGSize radii = CGSizeMake(kThisViewCornerRadius, kThisViewCornerRadius);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                           byRoundingCorners:corners
                                                 cornerRadii:radii];

// Mask the container view’s layer to round the corners.
CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer];
[cornerMaskLayer setPath:path.CGPath];
containerView.layer.mask = cornerMaskLayer;

// Make a transparent, stroked layer which will dispay the stroke.
CAShapeLayer *strokeLayer = [CAShapeLayer layer];
strokeLayer.path = path.CGPath;
strokeLayer.fillColor = [UIColor clearColor].CGColor;
strokeLayer.strokeColor = [UIColor redColor].CGColor;
strokeLayer.lineWidth = 2; // the stroke splits the width evenly inside and outside,
                           // but the outside part will be clipped by the containerView’s mask.

// Transparent view that will contain the stroke layer
UIView *strokeView = [[UIView alloc] initWithFrame:containerView.bounds];
strokeView.userInteractionEnabled = NO; // in case your container view contains controls
[strokeView.layer addSublayer:strokeLayer];

// configure and add any subviews to the container view

// stroke view goes in last, above all the subviews
[containerView addSubview:strokeView];

UIBezierPath UIRectCornerBottomLeft example.
- (void) drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
       byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10.0f, 10.0f)];

    [[UIColor blackColor] setFill];
    [path fill];

    CGRect innerRect = CGRectInset(rect, 4.0f, 2.0f);
    innerRect.origin.y -= 2.0f;

    UIBezierPath *innerPath = [UIBezierPath bezierPathWithRoundedRect:innerRect
        byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(8.0f, 8.0f)];

    [[UIColor redColor] setFill];
    [innerPath fill];
}

End of UIBezierPath UIRectCornerBottomLeft example article.