在這裡記錄下 我開發中常用的一些工具方法:
/**
* 將字串進行百分號編碼
*
* @param str 目標字串
*
* @return 經過百分號編碼後的字串
*/
+(NSString *)string2percentEncode:(NSString*)str{
return [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
複製程式碼
/**
* 擷取任意兩個字元之間的字串
*
* @param startRange 第一個條件字串
* @param endRange 第二個條件字串
* @param string 目標字串
*
* @return 擷取任意兩個字元之間字串
*/
+(NSString*)stringCutOffWithstartRange:(NSString *)startRange endRange:(NSString *)endRange andStr:(NSString *)string{
NSRange start = [string rangeOfString:startRange];
NSRange end = [string rangeOfString:endRange];
NSRange range = NSMakeRange(start.location + start.length, end.location - start.location - start.length);
return [string substringWithRange:range];
}
複製程式碼
/**
* 判斷字串是不是全是空格
*
* @param string 目標字串
*
* @return 返回是與否
*/
+ (BOOL)allSpace:(NSString *)string {
BOOL isAllSapce = NO;
// 獲取被空格分割後是空的字串的個數
NSInteger number = 0;
NSArray *array = [string componentsSeparatedByString:@" "];
for (NSString *str in array) {
if ([str isEqualToString:@""]) {
number++;
}
}
// 如果空的字串的個數等於 string.length+1,就說明全部是空格
if (number == string.length+1) {
isAllSapce = YES;
}
return isAllSapce;
}
複製程式碼
/**
* 將漢字轉化成拼音
*
* @param chinese 中文
*
* @return 小寫拼音
*/
+ (NSString *)transform:(NSString *)chinese{
NSMutableString *pinyin = [chinese mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
return pinyin;
}
複製程式碼
/**
* 將字串中的空格去掉再輸出
*
* @param chinese 目標字串
*
* @return 轉換後的字串
*/
+ (NSString *)deleteSpacesInString:(NSString *)string{
if ([string containsString:@" "]) {
return [string stringByReplacingOccurrencesOfString:@" " withString:@""];
} else {
return string;
}
}
複製程式碼
/**
* 判斷字串中是否包含數字
*
* @param chinese 目標字串
*
* @return 包含-->YES 不包含-->NO
*/
+(BOOL)isStringContainNumberWith:(NSString *)str{
NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
//count是str中包含[0-9]數字的個數,只要count>0,說明str中包含數字
if (count > 0) {
return YES;
}
return NO;
}
複製程式碼
/**
* 從字串中拿到第一組數字
*
* @param chinese 目標字串
*
* @return 返回數字字串
*/
+ (NSString *)getNumFromString:(NSString *)string{
//如果不包含數字 直接原樣返回
if (![HJTStringTool isStringContainNumberWith:string]) {
return string;
}
NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
int number;
[scanner scanInt:&number];
NSString *num=[NSString stringWithFormat:@"%d",number];
return num;
}
複製程式碼
/**
處理含有emoji編碼的的字串
@param string 目標字串
@return 返回帶有emoji表情的字串
*/
+ (NSString*)unmaskEmoji:(NSString *)string{
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:
@"\\[emoji:\\w{8}\\]"
options:0 error:nil];
NSArray * a = [regularExpression matchesInString:string options:0 range:NSMakeRange(0, string.length)];
NSString *resultString = string;
for (int i = a.count - 1; i >= 0; i --) {
NSTextCheckingResult *result = a[i];
NSRange range = NSMakeRange(result.range.location+7, result.range.length-8);
NSString *test = [string substringWithRange:range];
test = [self convertString2Unicode:test];
resultString = [resultString stringByReplacingCharactersInRange:result.range withString:test];
}
return resultString;
}
+ (NSString * )convertString2Unicode:(NSString *)input{
if (input.length && input.length == 8) {
unsigned short one = strtoul([[input substringWithRange:NSMakeRange(0, 4)] UTF8String],0,16);
unsigned short two = strtoul([[input substringWithRange:NSMakeRange(4, 4)] UTF8String],0,16);
return [NSString stringWithFormat:@"%C%C",one,two];
}else{
return input;
}
}
複製程式碼
/**
將JSON字串轉化為字典
@param JSONString 目標字串
@return 字典
*/
+(NSDictionary *)parseJSONStringToNSDictionary:(NSString *)JSONString {
NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
return responseJSON;
}
複製程式碼
//取消執行某個操作
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoHiddenNavAndBottom) object:nil];
複製程式碼
//適配iOS10字型變大的緣故
#define IOS_VERSION_10_OR_LATER (([[[UIDevice currentDevice]systemVersion]floatValue]>=10.0)? (YES):(NO))
#define DPAdapationLabelFont(n) (IOS_VERSION_10_OR_LATER?((n-1)*([[UIScreen mainScreen]bounds].size.width/375.0f)):(n*([[UIScreen mainScreen]bounds].size.width/375.0f)))
複製程式碼