BigAB
5/1/2013 - 6:37 PM

A category for UIView that takes the tedium out of manipulating frames (and bounds)

A category for UIView that takes the tedium out of manipulating frames (and bounds)

//
//  UIView+RectHelpers.m
//  


#import "UIView+RectHelpers.h"

@implementation UIView (RectHelpers)

// For frames
- (CGPoint)$origin { return self.frame.origin; }
- (void)set$origin:(CGPoint)origin { self.frame = (CGRect){ .origin=origin, .size=self.frame.size }; }

- (CGFloat)$x { return self.frame.origin.x; }
- (void)set$x:(CGFloat)x { self.frame = (CGRect){ .origin.x=x, .origin.y=self.frame.origin.y, .size=self.frame.size }; }

- (CGFloat)$y { return self.frame.origin.y; }
- (void)set$y:(CGFloat)y { self.frame = (CGRect){ .origin.x=self.frame.origin.x, .origin.y=y, .size=self.frame.size }; }

- (CGSize)$size { return self.frame.size; }
- (void)set$size:(CGSize)size { self.frame = (CGRect){ .origin=self.frame.origin, .size=size }; }

- (CGFloat)$width { return self.frame.size.width; }
- (void)set$width:(CGFloat)width { self.frame = (CGRect){ .origin=self.frame.origin, .size.width=width, .size.height=self.frame.size.height }; }

- (CGFloat)$height { return self.frame.size.height; }
- (void)set$height:(CGFloat)height { self.frame = (CGRect){ .origin=self.frame.origin, .size.width=self.frame.size.width, .size.height=height }; }

- (CGFloat)$left { return self.frame.origin.x; }
- (void)set$left:(CGFloat)left { self.frame = (CGRect){ .origin.x=left, .origin.y=self.frame.origin.y, .size.width=fmaxf(self.frame.origin.x+self.frame.size.width-left,0), .size.height=self.frame.size.height }; }

- (CGFloat)$top { return self.frame.origin.y; }
- (void)set$top:(CGFloat)top { self.frame = (CGRect){ .origin.x=self.frame.origin.x, .origin.y=top, .size.width=self.frame.size.width, .size.height=fmaxf(self.frame.origin.y+self.frame.size.height-top,0) }; }

- (CGFloat)$right { return self.frame.origin.x + self.frame.size.width; }
- (void)set$right:(CGFloat)right { self.frame = (CGRect){ .origin=self.frame.origin, .size.width=fmaxf(right-self.frame.origin.x,0), .size.height=self.frame.size.height }; }

- (CGFloat)$bottom { return self.frame.origin.y + self.frame.size.height; }
- (void)set$bottom:(CGFloat)bottom { self.frame = (CGRect){ .origin=self.frame.origin, .size.width=self.frame.size.width, .size.height=fmaxf(bottom-self.frame.origin.y,0) }; }

// For bounds
- (CGPoint)$borigin { return self.bounds.origin; }
- (void)set$borigin:(CGPoint)origin { self.bounds = (CGRect){ .origin=origin, .size=self.bounds.size }; }

- (CGFloat)$bx { return self.bounds.origin.x; }
- (void)set$bx:(CGFloat)x { self.bounds = (CGRect){ .origin.x=x, .origin.y=self.bounds.origin.y, .size=self.bounds.size }; }

- (CGFloat)$by { return self.bounds.origin.y; }
- (void)set$by:(CGFloat)y { self.bounds = (CGRect){ .origin.x=self.bounds.origin.x, .origin.y=y, .size=self.bounds.size }; }

- (CGSize)$bsize { return self.bounds.size; }
- (void)set$bsize:(CGSize)size { self.bounds = (CGRect){ .origin=self.bounds.origin, .size=size }; }

- (CGFloat)$bwidth { return self.bounds.size.width; }
- (void)set$bwidth:(CGFloat)width { self.bounds = (CGRect){ .origin=self.bounds.origin, .size.width=width, .size.height=self.bounds.size.height }; }

- (CGFloat)$bheight { return self.bounds.size.height; }
- (void)set$bheight:(CGFloat)height { self.bounds = (CGRect){ .origin=self.bounds.origin, .size.width=self.bounds.size.width, .size.height=height }; }

- (CGFloat)$bleft { return self.bounds.origin.x; }
- (void)set$bleft:(CGFloat)left { self.bounds = (CGRect){ .origin.x=left, .origin.y=self.bounds.origin.y, .size.width=fmaxf(self.bounds.origin.x+self.bounds.size.width-left,0), .size.height=self.bounds.size.height }; }

- (CGFloat)$btop { return self.bounds.origin.y; }
- (void)set$btop:(CGFloat)top { self.bounds = (CGRect){ .origin.x=self.bounds.origin.x, .origin.y=top, .size.width=self.bounds.size.width, .size.height=fmaxf(self.bounds.origin.y+self.bounds.size.height-top,0) }; }

- (CGFloat)$bright { return self.bounds.origin.x + self.bounds.size.width; }
- (void)set$bright:(CGFloat)right { self.bounds = (CGRect){ .origin=self.bounds.origin, .size.width=fmaxf(right-self.bounds.origin.x,0), .size.height=self.bounds.size.height }; }

- (CGFloat)$bbottom { return self.bounds.origin.y + self.bounds.size.height; }
- (void)set$bbottom:(CGFloat)bottom { self.bounds = (CGRect){ .origin=self.bounds.origin, .size.width=self.bounds.size.width, .size.height=fmaxf(bottom-self.bounds.origin.y,0) }; }



@end
//
//  UIView+RectHelpers.h
//
//  This awesome idea by Nick Farina (nfarina@gmail.com)
//

#import <UIKit/UIKit.h>

@interface UIView (RectHelpers)

@property (nonatomic, assign) CGPoint $origin;
@property (nonatomic, assign) CGSize $size;
@property (nonatomic, assign) CGFloat $x, $y, $width, $height; // normal rect properties
@property (nonatomic, assign) CGFloat $left, $top, $right, $bottom; // these will stretch/shrink the rect

// for bounds instead of frames
@property (nonatomic, assign) CGPoint $borigin;
@property (nonatomic, assign) CGSize $bsize;
@property (nonatomic, assign) CGFloat $bx, $by, $bwidth, $bheight; // normal rect properties
@property (nonatomic, assign) CGFloat $bleft, $btop, $bright, $bbottom; // these will stretch/shrink the rect

@end

A category for UIView that takes the tedium out of manipulating frames (and bounds)

Plucked from this project http://nfarina.com/post/29883229869/callout-view

Use it like now you can just go

myView$.height = 44.0f;
myView.$y = (myView.$y - myView.$height);

or whatever.