1、显示AlertView
+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:titlemessage:messagedelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alert show];
}
2、获取text的size
+ (CGSize)sizeOfText:(NSString *)text withFont:(UIFont *)font withWidth:(float) width{
CGSize size = [textsizeWithFont:fontconstrainedToSize:CGSizeMake(width,CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping];
return size;
}
3、得到当前的具体时间+(NSString *)getTodayFormatter{
NSDate *dateToday = [NSDatedate];
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY/MM/dd HH:mm:ss"];
[formatter setDateFormat:(@"yyyy-MM-dd")];
NSTimeZone* timeZone = [NSTimeZonesystemTimeZone];
[formatter setTimeZone:timeZone];
NSMutableString *dateStr = [NSMutableStringstringWithString: [formatterstringFromDate:dateToday]];
return [NSStringstringWithFormat:@"%@",dateStr];
}
4、Unix时间转标准时间,即从14XXXXXX转到2015年X月X日
+(NSString *)unixTimeToNSStringTime:(NSString *)unixTime{
double unixTimeStamp = [unixTimedoubleValue];
NSTimeInterval _interval=unixTimeStamp;
NSDate *date = [NSDatedateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatteralloc]init];
[_formatter setLocale:[NSLocalecurrentLocale]];
[_formatter setDateFormat:@"yyy-MM-dd HH:mm"];//[_formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
NSString *_strDate=[_formatterstringFromDate:date];
return _strDate;
}
5、检查电话号码
+(BOOL)checkTel:(NSString *)str{
if ([strlength] ==0) {
returnNO;
}
NSString *regex =@"^((13[0-9])|(147)|(15[^4,\\D]|(14[0-9])|(17[0-9]))|(18[0-9]))\\d{8}$";
NSPredicate *pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [predevaluateWithObject:str];
if (!isMatch) {
returnNO;
}
returnYES;
}
6、将电话号码中间4位用*代替
+(NSString *)replacePhoneNumberMiddleWithStar:(NSString *)strPhone{
NSRange ran =NSMakeRange(strPhone.length -8,4);
NSString *sub1 = [strPhonesubstringWithRange:ran];
strPhone = [strPhone stringByReplacingOccurrencesOfString:sub1withString:@"****"];
return strPhone;
}
7、判断字典中是否有nil,如果有就用@“”代替,免得nil报错
+(NSDictionary *)replaceNSDictionaryValue:(NSDictionary *)dictOrgin{
NSDictionary *info = (NSDictionary *)dictOrgin;
NSMutableDictionary *mudict = [NSMutableDictionarydictionary];
for (NSString * keyin info) {
NSString *strValue = [infoobjectForKey:key];
if (!strValue ) {
strValue = @"";
}
strValue = [NSStringstringWithFormat:@"%@",strValue];
if ( [strValuerangeOfString:@"null"].length >0) {
strValue = @"";
}
[mudict setObject:strValueforKey:key];
}
info = [mudict copy];
return info;
}
8、NSString判断是否为空
+(BOOL)IsNull:(NSString *)ranString{
ranString = ranString = [NSStringstringWithFormat:@"%@",ranString];
NSRange ran = [ranStringrangeOfString:@"null"];
if (ran.length >1) {
returnYES;
}
returnNO;
}
9、如果NSString为空,则用@“”代替
+(NSString *)replaceStringNull:(NSString *)ranString{
NSString *resultStr = [NSStringstringWithFormat:@"%@",ranString];
NSRange ran = [resultStrrangeOfString:@"null"];
if (ran.length >1) {
resultStr = [resultStr stringByReplacingOccurrencesOfString:@"null"withString:@""];
}
ran = [resultStr rangeOfString:@"()"];
if (ran.length >1) {
resultStr = [resultStr stringByReplacingOccurrencesOfString:@"()"withString:@""];
}
ran = [resultStr rangeOfString:@"<>"];
if (ran.length >1) {
resultStr = [resultStr stringByReplacingOccurrencesOfString:@"<>"withString:@""];
}
return resultStr;
}
10、替换带哦html标签,如<p>XXXXXXXXX</p>
+(NSString *)replaceHtmlTag:(NSString *)repString{
NSString *strTag =@" ";
NSRange ran = [repStringrangeOfString:strTag];
if (ran.length >0) {
repString = [repString stringByReplacingOccurrencesOfString:strTagwithString:@" "];
repString = [repString stringByReplacingOccurrencesOfString:@" "withString:@" "];
}
return repString;
}
11、打电话
+(void)makeCall:(NSString *)phoneNumber{
NSString *deviceType = [UIDevicecurrentDevice].model;
if([deviceType isEqualToString:@"iPod touch"]||[deviceType isEqualToString:@"iPad"]||[deviceType isEqualToString:@"iPhone Simulator"]){//
UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:@"您的设备不能打电话"delegate:nilcancelButtonTitle:@"好的,知道了"otherButtonTitles:nil,nil];
[alert show];
return;
}
NSString *tel = phoneNumber ;
NSString *num = [[NSStringalloc]initWithFormat:@"tel://%@",tel];
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:num]];
}
12、根据经纬度计算距离
#define PI 3.1415926
+ (double) LantitudeLongitudeDist:(double)lon1 other_Lat:(double)lat1 self_Lon:(double)lon2 self_Lat:(double)lat2{
double er =6378137;// 6378700.0f;
//ave. radius = 6371.315 (someone said more accurate is 6366.707)
//equatorial radius = 6378.388
//nautical mile = 1.15078
double radlat1 =PI*lat1/180.0f;
double radlat2 =PI*lat2/180.0f;
//now long.
double radlong1 =PI*lon1/180.0f;
double radlong2 =PI*lon2/180.0f;
if( radlat1 <0 ) radlat1 =PI/2 +fabs(radlat1);// south
if( radlat1 >0 ) radlat1 =PI/2 -fabs(radlat1);// north
if( radlong1 <0 ) radlong1 =PI*2 -fabs(radlong1);//west
if( radlat2 <0 ) radlat2 =PI/2 +fabs(radlat2);// south
if( radlat2 >0 ) radlat2 =PI/2 -fabs(radlat2);// north
if( radlong2 <0 ) radlong2 =PI*2 -fabs(radlong2);// west
//spherical coordinates x=r*cos(ag)sin(at), y=r*sin(ag)*sin(at), z=r*cos(at)
//zero ag is up so reverse lat
double x1 = er *cos(radlong1) *sin(radlat1);
double y1 = er *sin(radlong1) *sin(radlat1);
double z1 = er *cos(radlat1);
double x2 = er *cos(radlong2) *sin(radlat2);
double y2 = er *sin(radlong2) *sin(radlat2);
double z2 = er *cos(radlat2);
double d =sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
//side, side, side, law of cosines and arccos
double theta =acos((er*er+er*er-d*d)/(2*er*er));
double dist = theta*er;
return dist /1000;
}
13、16进制颜色转换成UIColor
+ (UIColor *) colorWithHexString: (NSString *)color
{
NSString *cString = [[colorstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]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 r, g, b;
[[NSScannerscannerWithString:rString]scanHexInt:&r];
[[NSScannerscannerWithString:gString]scanHexInt:&g];
[[NSScannerscannerWithString:bString]scanHexInt:&b];
return [UIColorcolorWithRed:((float) r /255.0f) green:((float) g /255.0f)blue:((float) b /255.0f)alpha:1.0f];
}
14、使图片显示成聊天气泡样式
转自:http://blog.csdn.net/kevinpake/article/details/41205715
15、tableview 跳转到最下面
NSIndexPath *lastPath = [NSIndexPath indexPathForRow:self.array.count - 1 inSection:0];
[self.tableView scrollToRowAtIndexPath:lastPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];