在这里记录下 我开发中常用的一些工具方法:
/**
* 将字符串进行百分号编码
*
* @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)))