1.html向字符串转化:
+(NSAttributedString*)htmlStr:(NSString *)string{
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
return attrStr;
}
2.检验输入框中是否包含表情输入:
+(BOOL)strContainsEmoji:(NSString *)string {
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
3.计算两个日期之间相差分钟数:
+(int)getMinutesFrom:(NSString *)fDate to:(NSString *)tDate{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//创建了两个日期对象
NSDate *date1=[dateFormatter dateFromString:fDate];
NSDate *date2=[dateFormatter dateFromString:tDate];
//NSDate *date=[NSDate date];
//NSString *curdate=[dateFormatter stringFromDate:date];
//取两个日期对象的时间间隔:
//这里的NSTimeInterval 并不是对象,是基本型,其实是double类型,是由c定义的:typedef double NSTimeInterval;
NSTimeInterval time=[date2 timeIntervalSinceDate:date1];
//int days=((int)time)/(3600*24);
//int hours=((int)time)%(3600*24)/3600;
//NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时",days,hours];
int minutes=((int)time)/(60);
return minutes;
}
4.UITableView设置多余的cell不显示方法:
+(void)removeExcessCellOfTableView:(UITableView *)table{
UIView *divisionView = [[UIView alloc] initWithFrame:CGRectZero];
[table setTableFooterView:divisionView];
}
5.IOS画虚线:
cgrect=CGRectMake(0, _cellHeight-1, _cellWidth, 1);
UIView *lineView=[[UIView alloc] initWithFrame:cgrect];
CAShapeLayer *shapeLayer=[Tools drawDottedLine:lineView color:UIColorFromRGB(wash_detail_dotted_line_color)];
[[self layer] addSublayer:shapeLayer];
[self.contentView addSubview:lineView];
+(CAShapeLayer*)drawDottedLine:(UIView *)view color:(UIColor*)color{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:view.bounds];
[shapeLayer setPosition:view.center];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
// 设置虚线颜色为
[shapeLayer setStrokeColor:[color CGColor]];
// 3.0f设置虚线的宽度
[shapeLayer setLineWidth:1.0f];
[shapeLayer setLineJoin:kCALineJoinRound];
// 3=线的宽度 1=每条线的间距
[shapeLayer setLineDashPattern:
[NSArray arrayWithObjects:[NSNumber numberWithInt:3],
[NSNumber numberWithInt:1],nil]];
//Setup the path CGMutablePathRef path = CGPathCreateMutable();
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
//CGPathAddLineToPoint(path, NULL, 320,0);
CGPathAddLineToPoint(path, NULL, ScreenWidth,0);
// Setup the pathCGMutablePathRef path = CGPathCreateMutable(); // 0,10代表初始坐标的x,y
// 320,10代表初始坐标的x,y CGPathMoveToPoint(path, NULL, 0, 10);
//CGPathAddLineToPoint(path, NULL, 320,10);
[shapeLayer setPath:path];
CGPathRelease(path);
return shapeLayer;
}
6.返回字符串的尺寸:
+(CGSize)sizeWithFont:(UIFont *)font str:(NSString*)str{
CGSize cgSize = [str sizeWithAttributes:@{ NSFontAttributeName : font }];
return cgSize;
}
7.判断一个字符串是否包含汉字:
//判断一个字符串是否包含汉字
+(BOOL)isContainChinese:(NSString*)str{
BOOL flag=NO;
for(int i=0; i< [str length];i++){
int a = [str characterAtIndex:i];
if(a>0x4e00&&a<0x9fff){
flag=YES;
break;
}
}
return flag;
}
8.设置UISearchBar的style:
+(void)setUISearchBarStyle:(UISearchBar *)searchBar{
searchBar.tintColor=[UIColor blackColor];
for (UIView *view in searchBar.subviews) {
// for before iOS7.0
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[view removeFromSuperview];
break;
}
// for later iOS7.0(include)
if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
[[view.subviews objectAtIndex:0] removeFromSuperview];
break;
}
}
}
9.获取当前时间:
+(NSString*)getCurrentTime{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateTime=[formatter stringFromDate:[NSDate date]];
return dateTime;
}
10.将日期格式转化成字符串:
+(NSString*)getFormatedTime:(NSDate*)date{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateTime=[formatter stringFromDate:date];
return dateTime;
}
11.改变图片大小:
//改变图片大小
+(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize{
UIGraphicsBeginImageContext(reSize);
[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
12.图片质量压缩:
+(UIImage*)compressImg:(UIImage*)img quality:(float)qly{
NSData *data = UIImageJPEGRepresentation(img, qly);
UIImage *image = [UIImage imageWithData:data];
return image;
}
13.设置UIView边框:
+(void)setViewBorder:(UIView *)view color:(UIColor *)color radius:(float)radius border:(float)border{
//设置layer
CALayer *layer=[view layer];
//是否设置边框以及是否可见
[layer setMasksToBounds:YES];
//设置边框圆角的弧度
[layer setCornerRadius:radius];
//设置边框线的宽
[layer setBorderWidth:border];
//设置边框线的颜色
[layer setBorderColor:[color CGColor]];
}
14.将图标设置成圆形:
+(void)setViewRoundWithImg:(UIView*)view img:(UIImage *)image{
[view.layer setCornerRadius:CGRectGetHeight([view bounds]) / 2];
view.layer.masksToBounds = YES;
view.layer.borderWidth = 1;
view.layer.borderColor = [[UIColor clearColor] CGColor];
view.layer.contents = (id)[image CGImage];
}
15.对象转二进制NSData:
//对象转NSData:对象必须实现NSCoding
+(NSData *)objectToData:(NSObject *)object{
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:object forKey:@"key"];//key是自定义的
[archiver finishEncoding];
return data;
}
16.NSData转对象:
//NSData转对象:对象必须实现NSCoding
+(NSObject *)dataToObject:(NSData *)data{
NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSObject *object = [unArchiver decodeObjectForKey:@"key"];//key是自定义的与上面设置的应相同
[unArchiver finishDecoding];
return object;
}
17.图片虚化处理:
+(UIImage *)blurrImage:(UIImage *) _image scale:(float)_scale;{
// Make sure that we have an image to work with
if (!_image)
return nil;
// Create context
CIContext *context = [CIContext contextWithOptions:nil];
// Create an image
CIImage *image = [CIImage imageWithCGImage:_image.CGImage];
// Set up a Gaussian Blur filter
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setValue:image forKey:kCIInputImageKey];
// Get blurred image out
CIImage *blurredImage = [blurFilter valueForKey:kCIOutputImageKey];
// Set up vignette filter
CIFilter *vignetteFilter = [CIFilter filterWithName:@"CIVignette"];
[vignetteFilter setValue:blurredImage forKey:kCIInputImageKey];
[vignetteFilter setValue:@(3.f) forKey:@"InputIntensity"];
// get vignette & blurred image
CIImage *vignetteImage = [vignetteFilter valueForKey:kCIOutputImageKey];
//CGFloat scale = [[UIScreen mainScreen] scale];
CGSize scaledSize = CGSizeMake(_image.size.width * _scale, _image.size.height * _scale);
CGImageRef imageRef = [context createCGImage:vignetteImage fromRect:(CGRect){CGPointZero, scaledSize}];
return [UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
}
18.设置自定义的提示框:
+(void)showMessage:(NSString *)message{
CGFloat SCREEN_WIDTH=[[UIScreen mainScreen] bounds].size.width;
CGFloat SCREEN_HEIGHT=[[UIScreen mainScreen] bounds].size.height;
UIWindow * window = [UIApplication sharedApplication].keyWindow;
UIView *showview = [[UIView alloc]init];
showview.backgroundColor = [UIColor blackColor];
showview.frame = CGRectMake(1, 1, 1, 1);
showview.alpha = 1.0f;
showview.layer.cornerRadius = 5.0f;
showview.layer.masksToBounds = YES;
[window addSubview:showview];
UILabel *label = [[UILabel alloc]init];
CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
label.text = message;
label.textColor = [UIColor whiteColor];
label.textAlignment = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:15];
[showview addSubview:label];
showview.frame = CGRectMake((SCREEN_WIDTH - LabelSize.width - 20)/2, SCREEN_HEIGHT/2, LabelSize.width+20, LabelSize.height+10);
[UIView animateWithDuration:2 animations:^{
showview.alpha = 0;
} completion:^(BOOL finished) {
[showview removeFromSuperview];
}];
}
19.字符串转数字:
//字符串数字转NSNumber
+(NSNumber*)StringToNumber:(NSString*) str{
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterNoStyle];
NSNumber * myNumber = [f numberFromString:str];
return myNumber;
}