Skip to content

Instantly share code, notes, and snippets.

@wannyk
Created July 31, 2015 07:51
Show Gist options
  • Save wannyk/dc83ffa41214d41fa2e3 to your computer and use it in GitHub Desktop.
Save wannyk/dc83ffa41214d41fa2e3 to your computer and use it in GitHub Desktop.
CGSizeAspectFit & CGSizeAspectFill from http://stackoverflow.com/a/17948778
CGSize CGSizeAspectFit(CGSize aspectRatio, CGSize boundingSize)
{
float mW = boundingSize.width / aspectRatio.width;
float mH = boundingSize.height / aspectRatio.height;
if( mH < mW )
boundingSize.width = boundingSize.height / aspectRatio.height * aspectRatio.width;
else if( mW < mH )
boundingSize.height = boundingSize.width / aspectRatio.width * aspectRatio.height;
return boundingSize;
}
CGSize CGSizeAspectFill(CGSize aspectRatio, CGSize minimumSize)
{
float mW = minimumSize.width / aspectRatio.width;
float mH = minimumSize.height / aspectRatio.height;
if( mH > mW )
minimumSize.width = minimumSize.height / aspectRatio.height * aspectRatio.width;
else if( mW > mH )
minimumSize.height = minimumSize.width / aspectRatio.width * aspectRatio.height;
return minimumSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment