//计算字符串中单词的个数 1 + (int)countWords:(NSString*)s 2 { 3 int i,n=[s length],l=0,a=0,b=0; 4 unichar c; 5 for(i=0;i<n;i++){ 6 c=[s characterAtIndex:i]; 7 if(isblank(c)) 8 { 9 b++; 10 }else if(isascii(c)) 11 { 12 a++; 13 }else 14 { 15 l++; 16 } 17 } 18 if(a==0 && l==0) 19 { 20 return 0; 21 } 22 return l+(int)ceilf((float)(a+b)/2.0); 23 }
//获取系统语言+ (NSString *)getLocalLanguage { NSString *language = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0]; return language; }
//获取本月,本周,本季度第一天的时间戳+ (unsigned long long)getFirstDayOfWeek:(unsigned long long)timestamp { NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:NSYearCalendarUnit| NSMonthCalendarUnit| NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit fromDate:now]; if (comps.weekday <2) { comps.week = comps.week-1; } comps.weekday = 2; NSDate *firstDay = [cal dateFromComponents:comps]; return [firstDay timeIntervalSince1970]; } + (unsigned long long)getFirstDayOfQuarter:(unsigned long long)timestamp { NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now]; if (comps.month <=3) { comps.month = 1; } else if(comps.month<=6) { comps.month = 4; } else if(comps.month<=9) { comps.month = 7; } else if(comps.month<=12) { comps.month = 10; } comps.day = 1; NSDate *firstDay = [cal dateFromComponents:comps]; return [firstDay timeIntervalSince1970]*1000; } + (unsigned long long)getFirstDayOfMonth:(unsigned long long)timestamp { NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp/1000]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now]; comps.day = 1; NSDate *firstDay = [cal dateFromComponents:comps]; return [firstDay timeIntervalSince1970]*1000; }
//监测网络状态 1 - (void)reachabilityChanged:(NSNotification *)note { 2 Reachability* curReach = [note object]; 3 NSParameterAssert([curReach isKindOfClass: [Reachability class]]); 4 NetworkStatus status = [curReach currentReachabilityStatus]; 5 6 if (status == NotReachable) 7 { 8 9 } 10 else if(status == kReachableViaWiFi) 11 { 12 13 } 14 else if(status == kReachableViaWWAN) 15 { 16 17 } 18 19 } 20 21 - (void)setNetworkNotification 22 { 23 [[NSNotificationCenter defaultCenter] addObserver:self 24 selector:@selector(reachabilityChanged:) 25 name: kReachabilityChangedNotification 26 object: nil]; 27 _hostReach = [[Reachability reachabilityWithHostName:@"http://www.baidu.com"] retain]; 28 [_hostReach startNotifier]; 29 }
//16进制颜色转UIColor 1 + (UIColor *)colorWithHex:(NSString *)hex { 2 // Remove `#` and `0x` 3 if ([hex hasPrefix:@"#"]) { 4 hex = [hex substringFromIndex:1]; 5 } else if ([hex hasPrefix:@"0x"]) { 6 hex = [hex substringFromIndex:2]; 7 } 8 9 // Invalid if not 3, 6, or 8 characters 10 NSUInteger length = [hex length]; 11 if (length != 3 && length != 6 && length != 8) { 12 return nil; 13 } 14 15 // Make the string 8 characters long for easier parsing 16 if (length == 3) { 17 NSString *r = [hex substringWithRange:NSMakeRange(0, 1)]; 18 NSString *g = [hex substringWithRange:NSMakeRange(1, 1)]; 19 NSString *b = [hex substringWithRange:NSMakeRange(2, 1)]; 20 hex = [NSString stringWithFormat:@"%@%@%@%@%@%@ff", 21 r, r, g, g, b, b]; 22 } else if (length == 6) { 23 hex = [hex stringByAppendingString:@"ff"]; 24 } 25 26 CGFloat red = [[hex substringWithRange:NSMakeRange(0, 2)] _hexValue] / 255.0f; 27 CGFloat green = [[hex substringWithRange:NSMakeRange(2, 2)] _hexValue] / 255.0f; 28 CGFloat blue = [[hex substringWithRange:NSMakeRange(4, 2)] _hexValue] / 255.0f; 29 CGFloat alpha = [[hex substringWithRange:NSMakeRange(6, 2)] _hexValue] / 255.0f; 30 31 return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 32 }
时间: 2024-10-06 06:28:21