// // NSString+EasyExtend.m // fastSign // // Created by EasyIOS on 14-4-10. // Copyright (c) 2014年 zhuchao. All rights reserved. // #import "NSString+EasyExtend.h" #import "NSData+EasyExtend.h" #import "NSObject+EasyTypeConversion.h" #import #import "pinyin.h" @implementation NSString (EasyExtend) //#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) //- (CGSize)sizeWithFont:(UIFont *)font byWidth:(CGFloat)width //{ // CGSize size = [self sizeWithFont:font // constrainedToSize:CGSizeMake(width, 999999.0f) // lineBreakMode:NSLineBreakByWordWrapping]; // return size; //} // //- (CGSize)sizeWithFont:(UIFont *)font byHeight:(CGFloat)height //{ // return [self sizeWithFont:font // constrainedToSize:CGSizeMake(999999.0f, height) // lineBreakMode:NSLineBreakByWordWrapping]; //} //#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) - (NSString *)MD5 { const char *str = [self UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(str, (CC_LONG)strlen(str), result); return [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]]; } - (NSString *)substringFromIndex:(NSUInteger)from untilCharset:(NSCharacterSet *)charset endOffset:(NSUInteger *)endOffset { if ( 0 == self.length ) return nil; if ( from >= self.length ) return nil; NSRange range = NSMakeRange( from, self.length - from ); NSRange range2 = [self rangeOfCharacterFromSet:charset options:NSCaseInsensitiveSearch range:range]; if ( NSNotFound == range2.location ) { if ( endOffset ) { *endOffset = range.location + range.length; } return [self substringWithRange:range]; } else { if ( endOffset ) { *endOffset = range2.location + range2.length; } return [self substringWithRange:NSMakeRange(from, range2.location - from)]; } } - (NSString *)trim { return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } - (NSArray *)words { #if ! __has_feature(objc_arc) NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease]; #else NSMutableArray *words = [[NSMutableArray alloc] init]; #endif const char *str = [self cStringUsingEncoding:NSUTF8StringEncoding]; char *word; for (int i = 0; i < strlen(str);) { int len = 0; if (str[i] >= 0xFFFFFFFC) { len = 6; } else if (str[i] >= 0xFFFFFFF8) { len = 5; } else if (str[i] >= 0xFFFFFFF0) { len = 4; } else if (str[i] >= 0xFFFFFFE0) { len = 3; } else if (str[i] >= 0xFFFFFFC0) { len = 2; } else if (str[i] >= 0x00) { len = 1; } word = malloc(sizeof(char) * (len + 1)); for (int j = 0; j < len; j++) { word[j] = str[j + i]; } word[len] = '\0'; i = i + len; NSString *oneWord = [NSString stringWithCString:word encoding:NSUTF8StringEncoding]; free(word); [words addObject:oneWord]; } return words; } - (NSString *)urldecode { return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; } //////////////////////////////////////////////////////////////////////////////////////////////////// - (NSString *)urlencode { NSString *encUrl = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSUInteger len = [encUrl length]; const char *c; c = [encUrl UTF8String]; NSString *ret = @""; for(int i = 0; i < len; i++) { switch (*c) { case '~': ret = [ret stringByAppendingString:@"%7E"]; break; case '/': ret = [ret stringByAppendingString:@"%2F"]; break; case '\'': ret = [ret stringByAppendingString:@"%27"]; break; case ';': ret = [ret stringByAppendingString:@"%3B"]; break; case '?': ret = [ret stringByAppendingString:@"%3F"]; break; case ':': ret = [ret stringByAppendingString:@"%3A"]; break; case '@': ret = [ret stringByAppendingString:@"%40"]; break; case '&': ret = [ret stringByAppendingString:@"%26"]; break; case '=': ret = [ret stringByAppendingString:@"%3D"]; break; case '+': ret = [ret stringByAppendingString:@"%2B"]; break; case '$': ret = [ret stringByAppendingString:@"%24"]; break; case ',': ret = [ret stringByAppendingString:@"%2C"]; break; case '[': ret = [ret stringByAppendingString:@"%5B"]; break; case ']': ret = [ret stringByAppendingString:@"%5D"]; break; case '#': ret = [ret stringByAppendingString:@"%23"]; break; case '!': ret = [ret stringByAppendingString:@"%21"]; break; case '(': ret = [ret stringByAppendingString:@"%28"]; break; case ')': ret = [ret stringByAppendingString:@"%29"]; break; case '*': ret = [ret stringByAppendingString:@"%2A"]; break; default: ret = [ret stringByAppendingFormat:@"%c", *c]; } c++; } return ret; } -(NSString *)getOutOfTheNumber{ NSMutableString *strippedString = [NSMutableString stringWithCapacity:self.length]; NSScanner *scanner = [NSScanner scannerWithString:self]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; while ([scanner isAtEnd] == NO) { NSString *buffer; if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) { [strippedString appendString:buffer]; } else { [scanner setScanLocation:([scanner scanLocation] + 1)]; } } return strippedString; } - (NSString *)urlByAppendingDict:(NSDictionary *)params encoding:(BOOL)encoding { NSURL * parsedURL = [NSURL URLWithString:self]; NSString * queryPrefix = parsedURL.query ? @"&" : @"?"; NSString * query = [NSString queryStringFromDictionary:params encoding:encoding]; return [NSString stringWithFormat:@"%@%@%@", self, queryPrefix, query]; } + (NSString *)queryStringFromDictionary:(NSDictionary *)dict encoding:(BOOL)encoding { NSMutableArray * pairs = [NSMutableArray array]; for ( NSString * key in dict.allKeys ) { NSString * value = [(NSObject *)[dict objectForKey:key] asNSString]; NSString * urlEncoding = encoding ? [value urlencode] : value; [pairs addObject:[NSString stringWithFormat:@"%@=%@", key, urlEncoding]]; } return [pairs componentsJoinedByString:@"&"]; } -(NSString *)getNameFromAddressBookWithPhoneNum{ ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); // if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) { // addressBook = ABAddressBookCreateWithOptions(NULL, NULL); // //等待同意后向下执行 // dispatch_semaphore_t sema = dispatch_semaphore_create(0); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { dispatch_semaphore_signal(sema); }); // dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); dispatch(sema); // }else{ // addressBook = ABAddressBookCreate(); // } CFArrayRef records; if (addressBook) { // 获取通讯录中全部联系人 records = ABAddressBookCopyArrayOfAllPeople(addressBook); }else{ return nil; } // 遍历全部联系人,检查是否存在指定号码 for (int i=0; i