Created
October 2, 2015 08:24
-
-
Save agustr/fe542a5d3b8d1579ff05 to your computer and use it in GitHub Desktop.
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
// | |
// 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