Wednesday, June 5, 2013

UIBezierPath UIRectCornerBottomRight example in Objective C (iOS).

UIBezierPath UIRectCornerBottomRight

The corners of a rectangle.

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

The top-left corner of the rectangle.
The top-right corner of the rectangle.
The bottom-left corner of the rectangle.
The bottom-right corner of the rectangle.
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerBottomRight]
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 UIRectCornerBottomRight example.
// Create the path (with only the bottom corners rounded)
    maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bodyBackgroundImageView.bounds
                                     byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                           cornerRadii:CGSizeMake(10.0, 10.0)];

    // Create the shape layer and set its path
    maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.titleBackgroundImageView.bounds;
    maskLayer.path = maskPath.CGPath;

    cell.bodyBackgroundImageView.layer.mask = maskLayer;

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

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

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:myView.bounds

// 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 UIRectCornerBottomRight 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 UIRectCornerBottomRight example article.