iOS - 工具函数
来源:互联网 发布:深圳网络安防培训学校 编辑:程序博客网 时间:2024/04/26 00:12
1.时间 :
取得时间差:
- (double)GetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE{ double timeDiff =0.0; NSDateFormatter *formatters = [[NSDateFormatteralloc] init]; [formatters setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *dateS = [formattersdateFromString:timeS]; NSDateFormatter *formatterE = [[NSDateFormatteralloc] init]; [formatterE setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *dateE = [formatterEdateFromString:timeE]; timeDiff = [dateE timeIntervalSinceDate:dateS ]; return timeDiff;}
换算成几天几小时几分钟
- (NSString *)timeFormatted:(int)totalSeconds{ int seconds = totalSeconds %60; int minutes = (totalSeconds /60) % 60; int hours = totalSeconds /3600; if (hours <24) { return [NSStringstringWithFormat:@"%02d小时:%02d分:%02d秒",hours, minutes, seconds]; } else { int days = totalSeconds /86400; int hourss = (totalSeconds /60*60) %24; int minutess = (totalSeconds /60) % 60;// int secondss = totalSeconds % 60; return [NSStringstringWithFormat:@"%02d天:%02d小时:%02d分", days, hourss, minutess]; }}
2.颜色 :
合成颜色.
+ (UIColor*)colorWithHexString:(NSString *)color{ NSCharacterSet* set = [NSCharacterSetwhitespaceAndNewlineCharacterSet]; NSString* cString = [[colorstringByTrimmingCharactersInSet:set]uppercaseString]; // String should be 6 or 8 characters if ([cStringlength] < 6) { return [UIColorclearColor]; } // strip 0X if it appears if ([cStringhasPrefix:@"0X"]) { cString = [cString substringFromIndex:2]; } if ([cStringhasPrefix:@"#"]) { cString = [cString substringFromIndex:1]; } if ([cStringlength] != 6) { return [UIColorclearColor]; } // Separate into r, g, b substrings NSRange range; range.location =0; range.length =2; //r NSString *rString = [cStringsubstringWithRange:range]; //g range.location =2; NSString *gString = [cStringsubstringWithRange:range]; //b range.location =4; NSString *bString = [cStringsubstringWithRange:range]; // Scan values unsignedint red, green, blue, one = 1; [[NSScannerscannerWithString:rString] scanHexInt:&red]; [[NSScannerscannerWithString:gString] scanHexInt:&green]; [[NSScannerscannerWithString:bString] scanHexInt:&blue]; constfloat r = (((float)red) /255.0f); constfloat g = (((float)green) /255.0f); constfloat b = (((float)blue) /255.0f); constfloat i = (((float)one)); return [UIColorcolorWithRed:r green:gblue:b alpha:i];}
3.计算高度 :
//计算高度NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13]};CGSize contentSize = [chartMessage.content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
只要把这一段代码封装好就可以变成一个计算文件Label 宽高的方法了
4.三方alertView库添加参数 :
把ZSBlockAlertView这个第三方的库添加了url这个重要参数:
- (void)sdkUpdateTypeWithURL:(NSString *)url cancelButtonTitle:(NSString *)cancelStr sureButtonTitle:(NSString *)sureStr{ ZSBlockAlertView *alert = [[ZSBlockAlertView alloc] initWithTitle:@"游戏更新" message:@"游戏有新版本" delegate:nil cancelButtonTitle:cancelStr otherButtonTitles:sureStr, nil]; [alert setClickHandler:^(NSInteger index) { if (index == 0) { NSURL *appstoreURL = [NSURL URLWithString:url]; if([[UIApplication sharedApplication] canOpenURL:appstoreURL]) { [[UIApplication sharedApplication] openURL:appstoreURL]; } } }]; [alert show];}
调用:
[[SYSystemInfo shareSystemInfo] sdkUpdateTypeWithURL:self.forceUpDateUrl cancelButtonTitle:@"立即更新" sureButtonTitle:nil];
5.压缩图片
压缩图片,常常用来上传到服务器!
- (NSData *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;{ UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return UIImageJPEGRepresentation(newImage, 0.8);}
0 0
- iOS - 工具函数
- iOS 利用余弦函数实现卡片浏览工具
- ios 工具
- IOS:IOS工具网站
- 工具函数
- 工具函数
- 工具函数
- 工具函数
- 工具函数
- 工具函数
- ios开发工具souretree工具
- ios 函数
- Ios 程序开发工具
- ios 产品图工具
- IOS同屏工具
- ios效率开发工具
- ios越狱工具
- ios统计工具
- iOS7 - UIActionSheet 按钮不响应点击事件的解决方法
- 黑马程序员-java基础-异常总结
- 在Unity里点击按钮打开一个浏览器页面 点击关闭 关闭这个页面
- android JAR混淆
- Android 使用shareSDK进行第三方登录失败解决方法(微信,微博,qq)
- iOS - 工具函数
- JPA基本API描述
- Spark MLlib Deep Learning Neural Net(深度学习-神经网络)1.1
- js和jsp的执行顺序
- GeoServer和MapServer的对比[转]
- 高仿支付宝手势解锁(增强版)
- 输入输出XML和YAML文件
- 3.13 Anagrams
- C++ 窗口设计 实践项目2 个人所得税计算器