+ (UIColor* ) colorWithHex:(int)color {
float red = (color & 0xff000000) >> 24;
float green = (color & 0x00ff0000) >> 16;
float blue = (color & 0x0000ff00) >> 8;
float alpha = (color & 0x000000ff);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/255.0];
}
- (UIImage *)duplicate
{
CGImageRef newCgIm = CGImageCreateCopy(self.CGImage);
UIImage *newImage = [UIImage imageWithCGImage:newCgIm scale:self.scale orientation:self.imageOrientation];
CGImageRelease(newCgIm);
return newImage;
}
- (UIImage *)stretched
{
CGSize size = self.size;
UIEdgeInsets insets = UIEdgeInsetsMake(truncf(size.height-1)/2, truncf(size.width-1)/2, truncf(size.height-1)/2, truncf(size.width-1)/2);
return [self resizableImageWithCapInsets:insets];
}
- (UIImage *)antiAlias
{
int border = 1;
CGRect rect = CGRectMake(border, border, self.size.width-2*border, self.size.height-2*border);
UIImage *img = nil;
UIGraphicsBeginImageContext(CGSizeMake(rect.size.width,rect.size.height));
[self drawInRect:CGRectMake(-1, -1, self.size.width, self.size.height)];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(self.size);
[img drawInRect:rect];
UIImage* antiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return antiImage;
}
+ (UIImage *)imageWithColor:(UIColor *)color Size:(CGSize)size
{
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [image stretched];
}
+ (UIImage *)imageName:(NSString *)name
{
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:[NSString stringWithFormat:@"/%@",name]];
return [UIImage imageWithContentsOfFile:path];
}
- (float)x
{
return self.frame.origin.x;
}
- (float)y
{
return self.frame.origin.y;
}
- (float)w
{
return self.frame.size.width;
}
- (float)h
{
return self.frame.size.height;
}
- (void)setTagName:(NSString*)name
{
self.tag = [name hash];
}
- (UIView *)viewWithName:(NSString *)name
{
return [self viewWithTag:[name hash]];
}
- (void) autoResize:(UIViewAutoresizing)mask
{
self.autoresizingMask = mask;
self.autoresizesSubviews = YES;
}
- (void)setPosition:(CGRect)position
{
self.bounds = CGRectMake(0, 0, position.size.width, position.size.height);
self.center = CGPointMake(position.origin.x,position.origin.y);
}
+ (UIButton*) buttonWithTarget:(id)target action:(SEL)sel
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
return btn;
}
- (void) setTextN:(NSString*)n H:(NSString*)h D:(NSString*)d S:(NSString *)s
{
if ( n )
{
[self setTitle:n forState:UIControlStateNormal];
}
if ( h )
{
[self setTitle:h forState:UIControlStateHighlighted];
}
if ( d )
{
[self setTitle:d forState:UIControlStateDisabled];
}
if ( s )
{
[self setTitle:s forState:UIControlStateSelected];
}
}
- (void) setImageN:(NSString*)n H:(NSString*)h D:(NSString*)d S:(NSString *)s
{
if ( n )
{
[self setImage:[UIImage imageName:n] forState:UIControlStateNormal];
}
if ( h )
{
[self setImage:[UIImage imageName:h] forState:UIControlStateHighlighted];
}
if ( d )
{
[self setImage:[UIImage imageName:d] forState:UIControlStateDisabled];
}
if ( s )
{
[self setImage:[UIImage imageName:s] forState:UIControlStateSelected];
if ( h )
{
[self setImage:[UIImage imageName:h] forState:UIControlStateHighlighted | UIControlStateSelected];
}
}
}
-(NSString *) trimHead
{
NSInteger i;
NSCharacterSet *cs = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for(i = 0; i < [self length]; i++)
{
if ( ![cs characterIsMember: [self characterAtIndex: i]] ) break;
}
return [self substringFromIndex: i];
}
-(NSString *) trimTail
{
NSInteger i;
NSCharacterSet *cs = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for(i = [self length] -1; i >= 0; i--)
{
if ( ![cs characterIsMember: [self characterAtIndex: i]] ) break;
}
return [self substringToIndex: (i+1)];
}
- (NSString *) trimBoth
{
return [[self trimHead] trimTail];
}
- (NSString*)MD5
{
const char *ptr = [self UTF8String];
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(ptr, strlen(ptr), md5Buffer);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x",md5Buffer[i]];
return output;
}
- (BOOL)equals:(NSString *)str
{
return [self compare:str] == NSOrderedSame;
}
- (CGFloat)heightByFont:(UIFont *)font width:(CGFloat)width
{
return [self sizeWithFont:font
constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByWordWrapping].height;
}
- (id)objectForKeyNotNull:(id)key {
id object = [self objectForKey:key];
if (object == [NSNull null])
return nil;
return object;
}
- (CGFloat)heightByWidth:(CGFloat)width
{
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self);
CGSize targetSize = CGSizeMake(width, CGFLOAT_MAX);
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [self length]), NULL, targetSize, NULL);
CFRelease(framesetter);
return fitSize.height;
}