Skip to content

Instantly share code, notes, and snippets.

@nfarina
Created August 21, 2012 06:40
Show Gist options
  • Star 86 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save nfarina/3412730 to your computer and use it in GitHub Desktop.
Save nfarina/3412730 to your computer and use it in GitHub Desktop.
UIView Frame helper getter/setter category methods
#import <UIKit/UIKit.h>
@interface UIView (SMFrameAdditions)
@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 the rect
@end
#import "UIView+FrameAdditions.h"
@implementation UIView (SMFrameAdditions)
- (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) }; }
@end
@nfarina
Copy link
Author

nfarina commented Aug 21, 2012

Ever find yourself doing a lot of this:

CGRect frame = self.frame;
frame.size.width = 50;
self.frame = frame;

Because you can't assign directly to members of structs hidden by properties?

Well with these category methods, you can write it like you want:

self.$width = 50;

It's kind of like jQuery for UIViews!

But wait, there's more. Use the left/top/right/bottom properties to move an edge of your frame rect around without moving the other sides! Just like absolute-positioned CSS. It makes for easy stretching of things without having to do a lot of math. Don't you hate math??

@benzado
Copy link

benzado commented Aug 21, 2012

I love math!

@sveinungkb
Copy link

Nice solution! I'm not sure how well I like the $-character, but the category is great! I've done the same by adding methods like CGRectSetWidth etc, but this is cleaner.

@DenTelezhkin
Copy link

This solution is cleaner and can be used with CocoaPods:
https://github.com/AlexDenisov/FrameAccessor

@arturgrigor
Copy link

That's lovely, great work! Can you post it to CocoaPods please?

@mbigatti
Copy link

feels like PHP... :-) Anyway, do you think (CGRect){} is better than CGRectMake() ?

@bugeye
Copy link

bugeye commented Oct 15, 2013

The $ stands for the time you save, since time=money.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment