Skip to content

Instantly share code, notes, and snippets.

@ayakix
Created March 29, 2013 05:25
Show Gist options
  • Save ayakix/5268928 to your computer and use it in GitHub Desktop.
Save ayakix/5268928 to your computer and use it in GitHub Desktop.
Utility and Tips for iPhone programming ref: http://qiita.com/items/fcccf652fd51a2f9b010
+(UIColor*) hexToUIColor:(NSString *)hex
{
if(hex.length == 6)
hex = [@"FF" stringByAppendingString: hex];
NSScanner *colorScanner = [NSScanner scannerWithString:hex];
unsigned int color;
[colorScanner scanHexInt:&color];
CGFloat a = ((color & 0xFF000000) >> 24)/255.0f;
CGFloat r = ((color & 0x00FF0000) >> 16)/255.0f;
CGFloat g = ((color & 0x0000FF00) >> 8) /255.0f;
CGFloat b = (color & 0x000000FF) /255.0f;
return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
+(CGFloat)screenWidth
{
CGRect r = [[UIScreen mainScreen] bounds];
return r.size.width;
}
+(CGFloat)fullScreenHeight
{
CGRect r = [[UIScreen mainScreen] bounds];
return r.size.height;
}
+(CGFloat)screenHeight
{
CGRect r = [[UIScreen mainScreen] applicationFrame];
return r.size.height;
}
+(CGFloat)statusBarHeight
{
CGRect full = [[UIScreen mainScreen] bounds];
CGRect main = [[UIScreen mainScreen] applicationFrame];
return full.size.height - main.size.height;
}
+(CGFloat)navigationBarHeight
{
// Use this method in ViewController >> [[[self tabBarController] rotationgHeaderView] frame].size.height;
return 44.0;
}
+(CGFloat)tabBarHeight
{
// Use this method in ViewController >> [[[self tabBarController] rotationgFooterView] frame].size.height;
return 49.0;
}
+(CGFloat)contentHeight
{
return [LayoutUtil screenHeight] - [LayoutUtil navigationBarHeight];
}
+(CGFloat)contentY
{
return [LayoutUtil statusBarHeight] + [LayoutUtil navigationBarHeight];
}
+(BOOL)isAspectRatio3x2
{
return ([LayoutUtil fullScreenHeight] / [LayoutUtil screenWidth] == 3.0 / 2.0);
}
+(CGFloat)screenScale
{
return ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) ? [[UIScreen mainScreen] scale] : 1.0;
}
- (UIView*)getGoogleLogo
{
NSArray *subviews = [self subviews];
if (subviews && [subviews count] < 2) return nil;
UIView *logoView = [[self subviews] objectAtIndex:1];
if ([logoView isKindOfClass:[UIImageView class]] != YES) return nil;
if (logoView.frame.size.width != 69 || logoView.frame.size.height != 23) return nil;
return logoView;
}
#define _(key) NSLocalizedString(key, key)
+ (UIButton *)makeButtonWithTarget:(id)target action:(SEL)action image:(UIImage *)image
{
UIButton *button = [[UIButton alloc] init];
button.contentMode = UIViewContentModeScaleAspectFit;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
image = nil;
[image release];
return button;
}
+ (UIButton *)makeButtonWithTarget:(id)target action:(SEL)action imageName:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName];
return [self makeButtonWithTarget:target action:action image:image];
}
+ (UIButton *)makeButtonWithTarget:(id)target action:(SEL)action title:(NSString *)title
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.textColor = [UIColor blackColor];
button.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
[button setTitle:_(title) forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (CGFloat)sX
{
return self.frame.origin.x;
}
- (CGFloat)sY
{
return self.frame.origin.y;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (CGFloat)eX
{
return self.frame.origin.x + self.frame.size.width;
}
- (CGFloat)eY
{
return self.frame.origin.y + self.frame.size.height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment