Skip to content

Instantly share code, notes, and snippets.

@NSCabezon
Created November 17, 2016 16:45
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 NSCabezon/82ebd5a5d82cc0be275fff3eef20ea6b to your computer and use it in GitHub Desktop.
Save NSCabezon/82ebd5a5d82cc0be275fff3eef20ea6b to your computer and use it in GitHub Desktop.
public class FrameUtils: NSObject {
// Resize to
public static func resize(frame: CGRect, toSize size: CGSize) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y, width: size.width, height: size.height)
}
public static func resize(frame: CGRect, toWidth width: CGFloat, andHeight height: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y, width: width, height: height)
}
public static func resize(frame: CGRect, toWidth width: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y, width: width, height: frame.size.height)
}
public static func resize(frame: CGRect, toHeight height: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: height)
}
// Resize by delta
public static func resize(frame: CGRect, resizeByDeltaSize size: CGSize) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width + size.width, height: frame.size.height + size.height)
}
public static func resize(frame: CGRect, resizeByWidthDelta width: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width + width, height: frame.size.height)
}
public static func resize(frame: CGRect, resizeByHeightDelta height: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: frame.size.height + height)
}
// Move to
public static func move(frame: CGRect, toPosition point: CGPoint) -> CGRect {
return CGRect(x: point.x, y: point.y, width: frame.size.width, height: frame.size.height)
}
public static func move(frame: CGRect, toX x: CGFloat) -> CGRect {
return CGRect(x: x, y: frame.origin.y, width: frame.size.width, height: frame.size.height)
}
public static func move(frame: CGRect, toY y: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x, y: y, width: frame.size.width, height: frame.size.height)
}
// Move by deltas
public static func move(frame: CGRect, byDeltaX x: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x + x, y: frame.origin.y, width: frame.size.width, height: frame.size.height)
}
public static func move(frame: CGRect, byDeltaY y: CGFloat) -> CGRect {
return CGRect(x: frame.origin.x, y: frame.origin.y + y, width: frame.size.width, height: frame.size.height)
}
// Center
public static func center(frame: CGRect, inFrame parentFrame: CGRect) -> CGRect {
let x = parentFrame.origin.x + ((parentFrame.size.width - frame.size.width) / 2);
let y = parentFrame.origin.y + ((parentFrame.size.height - frame.size.height) / 2);
return self.move(frame: frame, toPosition: CGPoint(x: x, y: y))
}
public static func center(frame: CGRect, horizontallyInFrame parentFrame: CGRect) -> CGRect {
let x = parentFrame.origin.x + ((parentFrame.size.width - frame.size.width) / 2);
return self.move(frame: frame, toX: x)
}
public static func center(frame: CGRect, verticallyInFrame parentFrame: CGRect) -> CGRect {
let y = parentFrame.origin.y + ((parentFrame.size.height - frame.size.height) / 2);
return self.move(frame: frame, toY: y)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment