Simple CGRect utils #core-graphics #objective-c #utils #cgrect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef RECTUTILS_H | |
#define RECTUTILS_H | |
#include <CoreGraphics/CGGeometry.h> | |
CG_INLINE CGRect CGRectChangeSize(CGRect rect, CGSize size); | |
CG_INLINE CGRect CGRectChangeWidth(CGRect rect, CGFloat width); | |
CG_INLINE CGRect CGRectChangeHeight(CGRect rect, CGFloat height); | |
CG_INLINE CGRect CGRectChangeOrigin(CGRect rect, CGPoint origin); | |
CG_INLINE CGRect CGRectChangeY(CGRect rect, CGFloat y); | |
CG_INLINE CGRect CGRectChangeX(CGRect rect, CGFloat x); | |
CG_INLINE CGRect CGRectAspectFit(CGRect parentRect, CGSize childSize); | |
CG_INLINE CGRect CGRectAspectFill(CGRect parentRect, CGSize childSize); | |
CG_INLINE CGRect CGRectMakeFromOriginAndSize(CGPoint origin, CGSize size); | |
CG_INLINE CGSize CGSizeAspectFit(CGSize parentSize, CGSize childSize); | |
CG_INLINE CGSize CGSizeAspectFill(CGSize parentSize, CGSize childSize); | |
CG_INLINE CGSize CGSizeChangeHeigth(CGSize size, CGFloat height); | |
CG_INLINE CGSize CGSizeChangeWidth(CGSize size, CGFloat width); | |
/*** Definitions of inline functions. ***/ | |
CG_INLINE CGRect CGRectChangeSize(CGRect rect, CGSize size) | |
{ return CGRectMake(rect.origin.x, rect.origin.y, size.width, size.height); } | |
CG_INLINE CGRect CGRectChangeWidth(CGRect rect, CGFloat width) | |
{ return CGRectMake(rect.origin.x, rect.origin.y, width, rect.size.height); } | |
CG_INLINE CGRect CGRectChangeHeight(CGRect rect, CGFloat height) | |
{ return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, height); } | |
CG_INLINE CGRect CGRectChangeOrigin(CGRect rect, CGPoint origin) | |
{ return CGRectMake(origin.x, origin.y, rect.size.width, rect.size.height); } | |
CG_INLINE CGRect CGRectChangeY(CGRect rect, CGFloat y) | |
{ return CGRectMake(rect.origin.x, y, rect.size.width, rect.size.height); } | |
CG_INLINE CGRect CGRectChangeX(CGRect rect, CGFloat x) | |
{ return CGRectMake(x, rect.origin.y, rect.size.width, rect.size.height); } | |
CG_INLINE CGRect CGRectMakeFromOriginAndSize(CGPoint origin, CGSize size) | |
{ return CGRectMake(origin.x, origin.y, size.width, size.height); } | |
CG_INLINE CGSize CGSizeAspectFit(CGSize parentSize, CGSize childSize) | |
{ | |
return (parentSize.width / parentSize.height > childSize.width / childSize.height) ? | |
CGSizeMake(childSize.width * parentSize.height / childSize.height, parentSize.height) : | |
CGSizeMake(parentSize.width, childSize.height * parentSize.width / childSize.width); | |
} | |
CG_INLINE CGSize CGSizeAspectFill(CGSize parentSize, CGSize childSize) | |
{ | |
return (parentSize.width / parentSize.height > childSize.width / childSize.height) ? | |
CGSizeMake(parentSize.width, childSize.height * parentSize.width / childSize.width) : | |
CGSizeMake(childSize.width * parentSize.height / childSize.height, parentSize.height); | |
} | |
CG_INLINE CGRect CGRectAspectFit(CGRect parentRect, CGSize childSize) | |
{ | |
CGSize resultSize = CGSizeAspectFit(parentRect.size, childSize); | |
CGPoint resultOrigin = CGPointMake(parentRect.origin.x + (parentRect.size.width - resultSize.width) / 2.0, | |
parentRect.origin.y + (parentRect.size.height - resultSize.height) / 2.0); | |
return CGRectMakeFromOriginAndSize(resultOrigin, resultSize); | |
} | |
CG_INLINE CGRect CGRectAspectFill(CGRect parentRect, CGSize childSize) | |
{ | |
CGSize resultSize = CGSizeAspectFill(parentRect.size, childSize); | |
CGPoint resultOrigin = CGPointMake(parentRect.origin.x + (parentRect.size.width - resultSize.width) / 2.0, | |
parentRect.origin.y + (parentRect.size.height - resultSize.height) / 2.0); | |
return CGRectMakeFromOriginAndSize(resultOrigin, resultSize); | |
} | |
CG_INLINE CGSize CGSizeChangeHeigth(CGSize size, CGFloat height) | |
{ return CGSizeMake(size.width, height); } | |
CG_INLINE CGSize CGSizeChangeWidth(CGSize size, CGFloat width) | |
{ return CGSizeMake(width, size.height); } | |
#endif // RECTUTILS_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment