Skip to content

Instantly share code, notes, and snippets.

@NicholasPeterson
Last active December 17, 2015 13:59
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 NicholasPeterson/5621648 to your computer and use it in GitHub Desktop.
Save NicholasPeterson/5621648 to your computer and use it in GitHub Desktop.
Basic convenience methods i like to have in UIKit.
@interface UIColor (NPUIExtras)
/**
Generates a color with random values. (not guarenteed to be truely random)
*/
+ (UIColor *)randomColor;
/**
Used to construct a color using values from 0 - 255 rather then 0 - 1.
*/
+ (UIColor *)colorWithEasyRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
/**
Gray color with a density from (white) 0 - 1 (black).
*/
+ (UIColor *)grayColorWithDensity:(CGFloat)density;
/**
See additional recommended additions: https://github.com/ars/uicolor-utilities
*/
@end
@interface UILabel (NPUIExtras)
- (CGFloat)heightToFitWidth:(CGFloat)width;
@end
@interface CAGradientLayer (styling)
/**
Easy initializer
@param startColor The Color at the top of the gradient
@param endColor The Color at the bottom of the gradient
@returns Autoreleased gradient layer
*/
+ (CAGradientLayer *)layerWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor;
@end
@interface UIView (NPUIExtras)
@property CGPoint position;
@property CGFloat x;
@property CGFloat y;
@property CGSize size;
@property CGFloat width;
@property CGFloat height;
/**
Applies a gradient layer to the very back of the layer hiearchy. You can use preset gradient layers or call one liners to both create and apply gradients
@param gradient The CAGradientLayer to apply
*/
- (void)setGradientBackground:(CAGradientLayer *)gradient;
@end
@implementation UIColor (NPUIExtras)
+ (UIColor *)colorWithEasyRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {
return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1];
}
+ (UIColor *)grayColorWithDensity:(CGFloat)density {
density = 1-density;
return [UIColor colorWithRed:density green:density blue:density alpha:1];
}
+ (UIColor *)randomColor {
NSInteger red = rand() % 255;
NSInteger blue = rand() % 255;
NSInteger green = rand() % 255;
return [UIColor colorWithEasyRed:red green:green blue:blue];
}
@end
@implementation UILabel (NPUIExtras)
-(CGFloat)heightToFitWidth:(CGFloat)width
{
if (![self.text length]) {
return 0;
}
CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:self.lineBreakMode];
return size.height;
}
@end
@implementation CAGradientLayer (NPUIExtras)
+ (CAGradientLayer *)layerWithStartColor:(UIColor *)start endColor:(UIColor *)end {
CAGradientLayer *layer = [CAGradientLayer layer];
CGColorRef endColor = end.CGColor;
CGColorRef startColor = start.CGColor;
NSArray *colors = [NSArray arrayWithObjects:(id) startColor, endColor, nil];
layer.colors = colors;
return layer;
}
@end
@implementation UIView (NPUIExtras)
- (CGPoint)position {
return [self frame].origin;
}
- (void)setPosition:(CGPoint)position {
CGRect rect = [self frame];
rect.origin = position;
[self setFrame:rect];
}
- (CGFloat)x {
return [self frame].origin.x;
}
- (void)setX:(CGFloat)x {
CGRect rect = [self frame];
rect.origin.x = x;
self.frame = rect;
//[self setFrame:rect];
}
- (CGFloat)y {
return [self frame].origin.y;
}
- (void)setY:(CGFloat)y {
CGRect rect = [self frame];
rect.origin.y = y;
[self setFrame:rect];
}
- (CGSize)size {
return [self frame].size;
}
- (void)setSize:(CGSize)size {
CGRect rect = [self frame];
rect.size = size;
[self setFrame:rect];
}
- (CGFloat)width {
return [self frame].size.width;
}
- (void)setWidth:(CGFloat)width {
CGRect rect = [self frame];
rect.size.width = width;
[self setFrame:rect];
}
- (CGFloat)height {
return [self frame].size.height;
}
- (void)setHeight:(CGFloat)height {
CGRect rect = [self frame];
rect.size.height = height;
[self setFrame:rect];
}
#pragma mark - gradients
- (void)setGradientBackground:(CAGradientLayer *)gradient {
[gradient setBounds:[self bounds]];
[gradient setPosition:CGPointMake([self bounds].size.width / 2, [self bounds].size.height / 2)];
NSInteger position = 0;
if([self isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)self;
if (button.buttonType == UIButtonTypeRoundedRect && [UIDevice systemVersionIsGreaterThen:__IPHONE_6_0]) {
//A fix for iOS6 rounded buttons
position = 1;
}
}
[self.layer insertSublayer:gradient atIndex:position];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment