1. 给UIColor加的随机颜色的分类,randomcolor 方便项目调试时使用。
randomcolor.h 文件
#import
@interface UIColor (RandomColor)
+(UIColor*)randomColor;
@end
#import "randomcolor.h"
@implementation UIColor(RandomColor)
+(UIColor*)randomColor{
CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
@end
2.对NSdictionary 进行Mutable深拷贝的分类
NSDictionary-DeepMutableCopy.h
#import
/**
* @brief NSDictionary 深拷贝分类
*/
@interface NSDictionary(DeepMutableCopy)
/**
* @brief NSDictionary深拷贝
*/
-(NSMutableDictionary *)mutableDeepCopy;
@end
NSDictionary-DeepMutableCopy.m
#import "NSDictionary-DeepMutableCopy.h"
@implementation NSDictionary(DeepMutableCopy)
-(NSMutableDictionary *)mutableDeepCopy
{
NSMutableDictionary *ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
NSArray *keys = [self allKeys];
for (id key in keys)
{
id oneValue = [self valueForKey:key];
id oneCopy = nil;
if ([oneValue respondsToSelector:@selector(mutableDeepCopy)])
oneCopy = [oneValue mutableDeepCopy];
else if ([oneValue respondsToSelector:@selector(mutableCopy)])
oneCopy = [oneValue mutableCopy];
if (oneCopy == nil)
oneCopy = [oneValue copy];
[ret setValue:oneCopy forKey:key];
}
return ret;
}
@end
3.项目中使用的URL 如果包含中文是无法通过识别的,所以需要把带中文的URL 重新编码
NSString+URL.h
#import
@interface NSString (URL)
-(NSString*)URLEncodeString;
@end
NSString+URL.m
#import "NSString+URL.h"
@implementation NSString (URL)
-(NSString*)URLEncodeString{
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL,kCFStringEncodingUTF8));
return encodedString;
}
@end
4.UIColor的分类:从十六进制字符串获取颜色。
UIColor+HEX.h
#import
#define RGBA_COLOR(R, G, B, A) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:A]
#define RGB_COLOR(R, G, B) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:1.0f]
@interface UIColor (HEX)
+ (UIColor *)colorWithHexString:(NSString *)color;
//从十六进制字符串获取颜色,
//color:支持@“#123456”、 @“0X123456”、 @“123456”三种格式
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;
@end
UIColor+HEX.m
#import "UIColor+HEX.h"
@implementation UIColor (HEX)
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
//删除字符串中的空格
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6)
{
return [UIColor clearColor];
}
// strip 0X if it appears
//如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
if ([cString hasPrefix:@"0X"])
{
cString = [cString substringFromIndex:2];
}
//如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
if ([cString hasPrefix:@"#"])
{
cString = [cString substringFromIndex:1];
}
if ([cString length] != 6)
{
return [UIColor clearColor];
}
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}
//默认alpha值为1
+ (UIColor *)colorWithHexString:(NSString *)color
{
return [self colorWithHexString:color alpha:1.0f];
}
@end
5.label的分类 根据内容获取高度,多用于根据文字内容来确定cell高度。
用法:首先确定label的font和大小,不设置有默认值,然后传入一个cgsize,只要填写所需要的宽度,高度随意填写,会返回所需要的高和宽
UILabel+StringFrame.h
#import
@interface UILabel (StringFrame)
- (CGSize)boundingRectWithSize:(CGSize)size;
@end
UILabel+StringFrame.m
#import "UILabel+StringFrame.h"
@implementation UILabel (StringFrame)
- (CGSize)boundingRectWithSize:(CGSize)size
{
NSDictionary *attribute = @{NSFontAttributeName: self.font};
CGSize retSize = [self.text boundingRectWithSize:size
options:\
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return retSize;
}
@end
UIImage+grayImage.h
#import
@interface UIImage (grayImage)
+ (UIImage*)getGrayImage:(UIImage*)sourceImage;
@end
UIImage+grayImage.m
#import "UIImage+grayImage.h"
@implementation UIImage (grayImage)
+ (UIImage*)getGrayImage:(UIImage*)sourceImage
{
int width = sourceImage.size.width;
int height = sourceImage.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGBitmapByteOrderDefault);
CGColorSpaceRelease(colorSpace);
if (context == NULL) {
return nil;
}
CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
CGImageRef grayImageRef = CGBitmapContextCreateImage(context);
UIImage *grayImage = [UIImage imageWithCGImage:grayImageRef];
CGContextRelease(context);
CGImageRelease(grayImageRef);
return grayImage;
}
@end