Skip to content

Instantly share code, notes, and snippets.

@asif-mistry
Created November 23, 2017 05:23
Show Gist options
  • Save asif-mistry/420f570d58e59458a8c3117b1c77715d to your computer and use it in GitHub Desktop.
Save asif-mistry/420f570d58e59458a8c3117b1c77715d to your computer and use it in GitHub Desktop.
objc seed code
//
// Created on 4/16/13.
//
#import "UIColor+Enhance.h"
//#import "JDUtils.h"
@implementation UIColor (Enhance)
- (unsigned int)rgb {
CGFloat r, g, b, a;
[self getRed:&r green:&g blue:&b alpha:&a];
return (unsigned int)(((int)(r * 255) & 0xff) << 16 |
((int)(g * 255) & 0xff) << 8 |
((int)(b * 255) & 0xff));
}
- (unsigned int)rgba {
CGFloat r, g, b, a;
[self getRed:&r green:&g blue:&b alpha:&a];
return (unsigned int)(((int)(r * 255) & 0xff) << 24 |
((int)(g * 255) & 0xff) << 16 |
((int)(b * 255) & 0xff) << 8 |
((int)(a * 255) & 0xff));
}
- (NSString *)rgbHexStringRepresentation {
return [NSString stringWithFormat:@"0x%06x", [self rgb]];
}
- (NSString *)rgbaHexStringRepresentation {
return [NSString stringWithFormat:@"0x%08x", [self rgba]];
}
- (CGColorSpaceModel)colorSpaceModel {
return CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
}
- (BOOL)canProvideRGBComponents {
return (([self colorSpaceModel] == kCGColorSpaceModelRGB) ||
([self colorSpaceModel] == kCGColorSpaceModelMonochrome));
}
- (CGFloat)red {
NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use -red, -green, -blue");
const CGFloat *c = CGColorGetComponents(self.CGColor);
return c[0];
}
- (CGFloat)green {
NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use -red, -green, -blue");
const CGFloat *c = CGColorGetComponents(self.CGColor);
if ([self colorSpaceModel] == kCGColorSpaceModelMonochrome) return c[0];
return c[1];
}
- (CGFloat)blue {
NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use -red, -green, -blue");
const CGFloat *c = CGColorGetComponents(self.CGColor);
if ([self colorSpaceModel] == kCGColorSpaceModelMonochrome) return c[0];
return c[2];
}
- (CGFloat)alpha {
const CGFloat *c = CGColorGetComponents(self.CGColor);
return c[CGColorGetNumberOfComponents(self.CGColor)-1];
}
+ (UIColor*)orangeGradientTopColor {
return [UIColor orangeColor];
}
+ (UIColor*)orangeGradientBottomColor {
return [UIColor whiteColor];
}
+ (UIColor*)blueGradientTopColor {
return [UIColor blueColor];
}
+ (UIColor*)blueGradientBottomColor {
return [UIColor whiteColor];
}
+ (UIColor*)grayGradientTopColor {
return [UIColor grayColor];
}
+ (UIColor*)grayGradientBottomColor {
return [UIColor whiteColor];
}
+ (UIColor*)greenGradientTopColor {
return [UIColor greenColor];
}
+ (UIColor*)greenGradientBottomColor {
return [UIColor whiteColor];
}
+ (UIColor*)helpShimColor {
return [UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:0.7];
}
+ (UIColor*)helpEvenCellColor {
return [UIColor colorWithRed: 245.0/255.0 green: 245.0/255.0 blue: 245.0/255.0 alpha: 1.0];
}
+ (UIColor*)helpOddCellColor {
return [UIColor colorWithRed: 234.0/255.0 green: 235.0/255.0 blue: 236.0/255.0 alpha: 1.0];
}
+ (UIColor *)UIColorFromRGBA:(NSUInteger)rgba
{
CGFloat red = ((rgba & 0xFF000000) >> 24) / 255.0f;
CGFloat green = ((rgba & 0x00FF0000) >> 16) / 255.0f;
CGFloat blue = ((rgba & 0x0000FF00) >> 8) / 255.0f;
CGFloat alpha = (rgba & 0x000000FF) / 255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
+ (UIColor *) UIColorFromRGB:(NSUInteger)rgbValue
{
CGFloat red = ((rgbValue & 0xFF0000) >> 16) / 255.0f;
CGFloat green = ((rgbValue & 0x00FF00) >> 8) / 255.0f;
CGFloat blue = (rgbValue & 0x0000FF) / 255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
+ (UIColor*)dimColor
{
return [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
}
@end
//
// UIView+AutoLayout.m
//
#import "UIView+AutoLayout.h"
@implementation UIView (AutoLayout)
+(id)autoLayoutView
{
UIView *viewToReturn = [self new];
viewToReturn.translatesAutoresizingMaskIntoConstraints = NO;
return viewToReturn;
}
-(NSArray *)centerInView:(UIView*)superview
{
NSMutableArray *constraints = [NSMutableArray new];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
[superview addConstraints:constraints];
return [constraints copy];
}
-(NSLayoutConstraint *)centerInContainerOnAxis:(NSLayoutAttribute)axis
{
NSParameterAssert(axis == NSLayoutAttributeCenterX || axis == NSLayoutAttributeCenterY);
UIView *superview = self.superview;
NSParameterAssert(superview);
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:axis relatedBy:NSLayoutRelationEqual toItem:superview attribute:axis multiplier:1.0 constant:0.0];
[superview addConstraint:constraint];
return constraint;
}
-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView;
{
NSParameterAssert(peerView);
UIView *superview = [self commonSuperviewWithView:peerView];
NSAssert(superview,@"Can't create constraints without a common superview");
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:peerView attribute:attribute multiplier:1.0 constant:0.0];
[superview addConstraint:constraint];
return constraint;
}
-(NSArray*)pinToSuperviewEdges:(IRViewPinEdges)edges inset:(CGFloat)inset
{
UIView *superview = self.superview;
NSAssert(superview,@"Can't pin to a superview if no superview exists");
NSMutableArray *constraints = [NSMutableArray new];
if (edges & IRViewPinTopEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:inset]];
}
if (edges & IRViewPinLeftEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:inset]];
}
if (edges & IRViewPinRightEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-inset]];
}
if (edges & IRViewPinBottomEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-inset]];
}
[superview addConstraints:constraints];
return [constraints copy];
}
-(NSArray*)pinToSuperviewEdgesWithInset:(UIEdgeInsets)insets
{
UIView *superview = self.superview;
NSAssert(superview,@"Can't pin to a superview if no superview exists");
NSMutableArray *constraints = [NSMutableArray new];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:IRViewPinTopEdge inset:insets.top]];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:IRViewPinLeftEdge inset:insets.left]];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:IRViewPinBottomEdge inset:insets.bottom]];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:IRViewPinRightEdge inset:insets.right]];
return [constraints copy];
}
-(NSLayoutConstraint *)pinEdge:(NSLayoutAttribute)edge toEdge:(NSLayoutAttribute)toEdge ofView:(UIView*)peerView
{
return [self pinEdge:edge toEdge:toEdge ofView:peerView inset:0.0];
}
-(NSLayoutConstraint *)pinEdge:(NSLayoutAttribute)edge toEdge:(NSLayoutAttribute)toEdge ofView:(UIView *)peerView inset:(CGFloat)inset
{
UIView *superview = [self commonSuperviewWithView:peerView];
NSAssert(superview,@"Can't create constraints without a common superview");
NSAssert (edge >= NSLayoutAttributeLeft && edge <= NSLayoutAttributeBottom,@"Edge parameter is not an edge");
NSAssert (toEdge >= NSLayoutAttributeLeft && toEdge <= NSLayoutAttributeBottom,@"Edge parameter is not an edge");
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:edge relatedBy:NSLayoutRelationEqual toItem:peerView attribute:toEdge multiplier:1.0 constant:inset];
[superview addConstraint:constraint];
return constraint;
}
-(NSArray *)pinEdges:(IRViewPinEdges)edges toSameEdgesOfView:(UIView *)peerView
{
return [self pinEdges:edges toSameEdgesOfView:peerView inset:0];
}
-(NSArray *)pinEdges:(IRViewPinEdges)edges toSameEdgesOfView:(UIView *)peerView inset:(CGFloat)inset
{
UIView *superview = [self commonSuperviewWithView:peerView];
NSAssert(superview,@"Can't create constraints without a common superview");
NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:4];
if (edges & IRViewPinTopEdge)
{
[constraints addObject:[self pinEdge:NSLayoutAttributeTop toEdge:NSLayoutAttributeTop ofView:peerView inset:inset]];
}
if (edges & IRViewPinLeftEdge)
{
[constraints addObject:[self pinEdge:NSLayoutAttributeLeft toEdge:NSLayoutAttributeLeft ofView:peerView inset:inset]];
}
if (edges & IRViewPinRightEdge)
{
[constraints addObject:[self pinEdge:NSLayoutAttributeRight toEdge:NSLayoutAttributeRight ofView:peerView inset:-inset]];
}
if (edges & IRViewPinBottomEdge)
{
[constraints addObject:[self pinEdge:NSLayoutAttributeBottom toEdge:NSLayoutAttributeBottom ofView:peerView inset:-inset]];
}
[superview addConstraints:constraints];
return [constraints copy];
}
-(NSArray *)constrainToSize:(CGSize)size
{
NSMutableArray *constraints = [NSMutableArray new];
if (size.width)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:size.width]];
if (size.height)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:size.height]];
[self addConstraints:constraints];
return [constraints copy];
}
-(NSArray *)constrainToMinimumSize:(CGSize)minimum maximumSize:(CGSize)maximum
{
NSAssert(minimum.width <= maximum.width, @"maximum width should be strictly wider than or equal to minimum width");
NSAssert(minimum.height <= maximum.height, @"maximum height should be strictly higher than or equal to minimum height");
NSArray *minimumConstraints = [self constrainToMinimumSize:minimum];
NSArray *maximumConstraints = [self constrainToMaximumSize:maximum];
return [minimumConstraints arrayByAddingObjectsFromArray:maximumConstraints];
}
-(NSArray *)constrainToMinimumSize:(CGSize)minimum
{
NSMutableArray *constraints = [NSMutableArray array];
if (minimum.width)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:0 multiplier:0 constant:minimum.width]];
if (minimum.height)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:0 multiplier:0 constant:minimum.height]];
[self addConstraints:constraints];
return [constraints copy];
}
-(NSArray *)constrainToMaximumSize:(CGSize)maximum
{
NSMutableArray *constraints = [NSMutableArray array];
if (maximum.width)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:0 multiplier:0 constant:maximum.width]];
if (maximum.height)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:0 multiplier:0 constant:maximum.height]];
[self addConstraints:constraints];
return [constraints copy];
}
-(NSArray*)pinPointAtX:(NSLayoutAttribute)x Y:(NSLayoutAttribute)y toPoint:(CGPoint)point
{
UIView *superview = self.superview;
NSAssert(superview,@"Can't create constraints without a superview");
// Valid X positions are Left, Center, Right and Not An Attribute
BOOL xValid = (x == NSLayoutAttributeLeft || x == NSLayoutAttributeCenterX || x == NSLayoutAttributeRight || x == NSLayoutAttributeNotAnAttribute);
// Valid Y positions are Top, Center, Baseline, Bottom and Not An Attribute
BOOL yValid = (y == NSLayoutAttributeTop || y == NSLayoutAttributeCenterY || y == NSLayoutAttributeBaseline || y == NSLayoutAttributeBottom || y == NSLayoutAttributeNotAnAttribute);
NSAssert (xValid && yValid,@"Invalid positions for creating constraints");
NSMutableArray *constraints = [NSMutableArray array];
if (x != NSLayoutAttributeNotAnAttribute)
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:x relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:point.x];
[constraints addObject:constraint];
}
if (y != NSLayoutAttributeNotAnAttribute)
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:y relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:point.y];
[constraints addObject:constraint];
}
[superview addConstraints:constraints];
return [constraints copy];
}
-(void)spaceViews:(NSArray*)views onAxis:(UILayoutConstraintAxis)axis withSpacing:(CGFloat)spacing alignmentOptions:(NSLayoutFormatOptions)options;
{
NSAssert([views count] > 1,@"Can only distribute 2 or more views");
NSString *direction = nil;
switch (axis) {
case UILayoutConstraintAxisHorizontal:
direction = @"H:";
break;
case UILayoutConstraintAxisVertical:
direction = @"V:";
break;
default:
return;
}
UIView *previousView = nil;
NSDictionary *metrics = @{@"spacing":@(spacing)};
NSString *vfl = nil;
for (UIView *view in views)
{
vfl = nil;
NSDictionary *views = nil;
if (previousView)
{
vfl = [NSString stringWithFormat:@"%@[previousView(==view)]-spacing-[view]",direction];
views = NSDictionaryOfVariableBindings(previousView,view);
}
else
{
vfl = [NSString stringWithFormat:@"%@|-spacing-[view]",direction];
views = NSDictionaryOfVariableBindings(view);
}
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:options metrics:metrics views:views]];
previousView = view;
}
vfl = [NSString stringWithFormat:@"%@[previousView]-spacing-|",direction];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:options metrics:metrics views:NSDictionaryOfVariableBindings(previousView)]];
}
-(UIView*)commonSuperviewWithView:(UIView*)peerView
{
UIView *commonSuperview = nil;
UIView *startView = self;
do {
if ([peerView isDescendantOfView:startView])
{
commonSuperview = startView;
}
startView = startView.superview;
} while (startView && !commonSuperview);
return commonSuperview;
}
@end
//
// Created on 3/29/13.
//
#import "UIView+Enhance.h"
#import "UIColor+Enhance.h"
#import <objc/runtime.h>
static char kAssociatedObjectOriginalFrameKey;
@implementation UIView (Enhance)
- (CGRect) originalFrame
{
NSString *frame = objc_getAssociatedObject(self, &kAssociatedObjectOriginalFrameKey);
CGRect retValue = CGRectFromString(frame);
return retValue;
}
- (void) setOriginalFrame:(CGRect)originalFrame
{
NSString *frame = NSStringFromCGRect(originalFrame);
objc_setAssociatedObject(self, &kAssociatedObjectOriginalFrameKey, frame, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)setHeight:(CGFloat)height {
CGRect bounds = self.bounds;
bounds.size.height = height;
self.bounds = bounds;
}
- (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (void)setXLoc:(CGFloat)xLoc {
CGRect frame = self.frame;
frame.origin.x = xLoc;
self.frame = frame;
}
- (void)setCenterX:(CGFloat)centerX {
CGRect frame = self.frame;
self.center = CGPointMake(centerX, CGRectGetMidY(frame));
}
- (void)setTopLeft:(CGPoint)topLeft {
CGRect frame = self.frame;
frame.origin.x = topLeft.x;
frame.origin.y = topLeft.y;
self.frame = frame;
}
- (void)setTopRight:(CGPoint)topRight {
CGRect frame = self.frame;
frame.origin.x = topRight.x - frame.size.width;
frame.origin.y = topRight.y;
self.frame = frame;
}
- (CGFloat)width {
return CGRectGetWidth(self.bounds);
}
- (CGFloat)height {
return CGRectGetHeight(self.bounds);
}
- (UIView*) loadInstanceFromNib {
UIView *result = nil;
NSArray* elements = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class])
owner: nil
options: nil];
for (id anObject in elements) {
if ([anObject isKindOfClass:[self class]]) {
result = anObject;
break;
}
}
return result;
}
-(void)setAdjustedAnchorPointInPixels:(CGPoint)anchorPoint {
CGPoint pctPoint;
pctPoint.x = anchorPoint.x / self.bounds.size.width;
pctPoint.y = anchorPoint.y / self.bounds.size.height;
[self setAdjustedAnchorPoint:pctPoint];
}
-(void)setAdjustedAnchorPoint:(CGPoint)anchorPoint
{
CGPoint newPoint = CGPointMake(self.bounds.size.width * anchorPoint.x, self.bounds.size.height * anchorPoint.y);
CGPoint oldPoint = CGPointMake(self.bounds.size.width * self.layer.anchorPoint.x, self.bounds.size.height * self.layer.anchorPoint.y);
newPoint = CGPointApplyAffineTransform(newPoint, self.transform);
oldPoint = CGPointApplyAffineTransform(oldPoint, self.transform);
CGPoint position = self.layer.position;
position.x -= oldPoint.x;
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
self.layer.position = position;
self.layer.anchorPoint = anchorPoint;
}
- (UIImageView *) capture
{
CGFloat scale = 1.0;
if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
CGFloat tmp = [[UIScreen mainScreen]scale];
if (tmp > 1.5) {
scale = 2.0;
}
}
BOOL isHidden = self.hidden;
self.hidden = NO;
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.layer renderInContext:ctx];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIImageView * imgView = [[UIImageView alloc] initWithImage:img];
UIGraphicsEndImageContext();
self.hidden = isHidden;
return imgView;
}
- (void) setRoundedCornerRadius
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
cornerRadii:CGSizeMake(7, 7)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
[self.layer setMask:maskLayer];
}
@end
@implementation UIScreen (RetinaCheck)
+ (BOOL)isRetinaScreen
{
static BOOL isRetinaScreen = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// This is done inside the dispatch_once (and we are using the dispatch_once) because the results of these calls will not change
// during the execution of the app; So there is no need to execute these two calls more than one time.
isRetinaScreen = ([[self mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([self mainScreen].scale == 2.0));
});
return isRetinaScreen;
}
@end
void *const globalMainQueueIdentityKey = (void *)&globalMainQueueIdentityKey;
@interface JDUtility ()
@end
@implementation JDUtility
+ (void)load
{
ddLogLevel = COMBINED_LOG_LEVEL(ddLogLevel);
[JDUtility setMainQueueIdentity];
}
+ (BOOL)isOnMainQueue
{
return (dispatch_get_specific(globalMainQueueIdentityKey) != NULL);
}
+ (void) setMainQueueIdentity
{
void *nonNullValue = globalMainQueueIdentityKey; // Whatever, just not null
dispatch_queue_set_specific(dispatch_get_main_queue(), globalMainQueueIdentityKey, nonNullValue, NULL);
}
+ (void) safeExecuteOnMainThread:(void(^)(void))block {
if (![JDUtility isOnMainQueue])
{
dispatch_sync(dispatch_get_main_queue(), block);
}
else
{
block();
}
}
+ (NSMutableDictionary*)mutableDictionaryWithRetainedKeys
{
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
return CFBridgingRelease(dictionary);
}
+ (NSDate*)dateFromProtoLong:(long long)longDate {
NSTimeInterval interval = longDate/1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
return date;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment