Created
August 21, 2012 06:40
-
-
Save nfarina/3412730 to your computer and use it in GitHub Desktop.
UIView Frame helper getter/setter category methods
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
#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 |
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
#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 |
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.
This solution is cleaner and can be used with CocoaPods:
https://github.com/AlexDenisov/FrameAccessor
That's lovely, great work! Can you post it to CocoaPods please?
feels like PHP... :-) Anyway, do you think (CGRect){} is better than CGRectMake() ?
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
I love math!