Skip to content

Instantly share code, notes, and snippets.

@agustr
Created October 2, 2015 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustr/fe542a5d3b8d1579ff05 to your computer and use it in GitHub Desktop.
Save agustr/fe542a5d3b8d1579ff05 to your computer and use it in GitHub Desktop.
//
// CGSize+Utilities.swift
// STHLMPubCrawl
//
// Created by Agust Rafnsson on 02/10/15.
// Copyright © 2015 Agust Rafnsson. All rights reserved.
//
import UIKit
extension CGSize{
func scaledToFitContainerOfSize(size:CGSize)->CGSize{
if (self.height == 0 ) || (self.width == 0){
return CGSize(width: 0, height: 0)
}
let factor = self.resizeFactorToFit(size)
return CGSize(width: (self.width * factor), height: (self.height * factor))
}
func scaledToFillContainerOfSize(size:CGSize)->CGSize{
let factor = self.resizeFactorToFill(size)
return CGSize(width: (self.width * factor), height: (self.height * factor))
}
func resizeFactorToFit(size:CGSize)->CGFloat{
let widthFactor = size.width/self.width // the smaller will fit the larger will fill
let heightFactor = size.height/self.height
return min(widthFactor, heightFactor)
}
func resizeFactorToFill(size:CGSize)->CGFloat{
let widthFactor = size.width/self.width // the smaller will fit the larger will fill
let heightFactor = size.height/self.height
return max(widthFactor, heightFactor)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment