MBRoundedRectTools Class Reference

Inherits from NSObject
Declared in MBRoundedRectTools.h

Overview

A few tools for creating rounded rectangle paths and managing corner radii.

Corner radius utilities

+ cornerRadiusSizedForRect:

Returns a corner radius sized appropriately for the given rectangle.

+ (CGFloat)cornerRadiusSizedForRect:(CGRect)rect

Parameters

rect

The rectangle for which a corner radius is desired.

Return Value

A corner radius that should look good when used with rect. (Or, since “good” is subjective, a corner radius that shouldn’t look ridiculous when used with rect.)

Declared In

MBRoundedRectTools.h

+ adjustCornerRadius:fromRect:withInset:

Returns a corner radius that approximates the curvature of the corner radius supplied for the given rectangle when that rectangle is inset by the specified amount.

+ (CGFloat)adjustCornerRadius:(CGFloat)radius fromRect:(CGRect)rect withInset:(CGFloat)inset

Parameters

radius

The radius used with rect.

rect

The rectangle to be adjusted by inset.

inset

The number of points each edge of rect will be inset to create the rectangle used for calculating the returned corner radius. The width and height dimensions of rect are each reduced by 2 ✕ inset; a positive inset results in a rectangle smaller than rect, while a negative inset results in a larger rectangle.

Return Value

A corner radius for the adjusted rectangle.

Discussion

This can be used to create nested rounded rectangles of different sizes whose corner arcs look related.

Declared In

MBRoundedRectTools.h

+ adjustCornerRadius:fromRect:toRect:

Returns a corner radius for a rectangle that approximates the curvature of a corner radius used for a rectangle of a different size.

+ (CGFloat)adjustCornerRadius:(CGFloat)radius fromRect:(CGRect)fromRect toRect:(CGRect)toRect

Parameters

radius

The radius used with fromRect.

fromRect

The rectangle that uses the corner radius radius.

toRect

The rectangle for which the corner radius will be returned.

Return Value

A corner radius for toRect that approximates the curvature of the corner arc resulting from using radius with fromRect.

Discussion

This can be used to create nested rounded rectangles of different sizes whose corner arcs look related.

Declared In

MBRoundedRectTools.h

Creating rounded rectangle paths

+ newRoundedPathForRectangle:withRadius:

Creates a new path for a rounded rectangle. The path will be adjusted to ensure that on non-Retina displays, subpixel rendering will not occur.

+ (nonnull CGPathRef)newRoundedPathForRectangle:(CGRect)rect withRadius:(CGFloat)radius

Parameters

rect

The rectangle for which a rounded rectangle path is desired.

radius

The corner radius to be used by the returned path.

Return Value

A new CGPathRef usable for drawing the rounded rectangle path.

Discussion

Note: The caller takes ownership of the returned path and is responsible for ensuring its release.

Declared In

MBRoundedRectTools.h

+ newRoundedPathForRectangle:withRadius:forStrokeWidth:

Creates a new path for a rounded rectangle. The rectangle is adjusted to ensure that the stroke falls entirely within the bounds of the path, and that odd stroke widths do not result in subpixel rendering on non-Retina displays.

+ (nonnull CGPathRef)newRoundedPathForRectangle:(CGRect)rect withRadius:(CGFloat)radius forStrokeWidth:(CGFloat)width

Parameters

rect

The rectangle for which a rounded rectangle path is desired.

radius

The corner radius to be used by the returned path.

width

The stroke width that will be used when drawing the returned path.

Return Value

A new CGPathRef usable for drawing the rounded rectangle path.

Discussion

This variant ensures that the returned path is adjusted to account for the stroke width that will be used to draw it.

Note: The caller takes ownership of the returned path and is responsible for ensuring its release.

Declared In

MBRoundedRectTools.h

+ newRoundedPathForRectangle:withRadius:insetBy:

Creates a new path for a rounded rectangle. The rectangle is adjusted by the specified inset, but is not otherwise adjusted to account for stroke widths or subpixel rendering on non-Retina displays.

+ (nonnull CGPathRef)newRoundedPathForRectangle:(CGRect)rect withRadius:(CGFloat)radius insetBy:(CGFloat)inset

Parameters

rect

The rectangle for which a rounded rectangle path is desired.

radius

The corner radius to be used by the returned path.

inset

The number of points each edge of rect will be inset to create the rounded rectangle path. The width and height dimensions of rect are each reduced by 2 ✕ inset; a positive inset results in a rectangle smaller than rect, while a negative inset results in a larger rectangle.

Return Value

A new CGPathRef usable for drawing the rounded rectangle path.

Discussion

Note: The caller takes ownership of the returned path and is responsible for ensuring its release.

Declared In

MBRoundedRectTools.h