#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface NSString (Extension)
//根据字体返回宽高
- (CGSize)textSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode;
//是否是子串
- (BOOL)isContainSubString:(NSString *)subString;
//是否是表情
- (BOOL)stringContainsEmoji;
//是否是手机号
- (BOOL)isMobileNumber;
//是否是电话号码
//- (BOOL)isFixPhoneNumber;
//是否是邮箱
- (BOOL)isAvailableEmail;
//是否是纯数字
- (BOOL)isNumText;
/**
是否是身份证
*/
- (BOOL)isIdentityNumber;
//最多显示两位小数
+(NSString *)returnActualFormatNumber:(double)number;
//保留几位小数
+(NSString *)returnDetailFormatNumber:(double)number leaveCount:(NSInteger)count;
//设置富文本
+ (NSMutableAttributedString *)setNbLabelTextProperty:(NSString *)text
string:(NSString *)string
textColor:(UIColor *)color
textFont:(UIFont *)font;
+ (NSMutableAttributedString *)attributeStringWithText:(NSString *)text
withLineSpace:(CGFloat )lineSpace
withLineBreakMode:(NSLineBreakMode )lineBreakMode
withFont:(UIFont *)font;
+ (NSMutableAttributedString *)attributebLabelText:(NSString *)text
withTextColor:(UIColor *)textColor
withLineSpace:(CGFloat)lineSpace
withTextFont:(UIFont *)textFont
withExtraStringArray:(NSArray *)extraStringArr
withExtraColorArray:(NSArray *)extraColorArr
withExtraFontArray:(NSArray *)extraFont;
/**
是否是身份证
*/
+ (BOOL)validateIdentityCard:(NSString *)identityCard;
//是否有空格
- (BOOL)isHaveSpaceInString;
//是否有中文字符
- (BOOL)isHaveChineseInString;
//是否是url
- (BOOL)isWebUrlStr;
@end
#import "NSString+Extension.h"
@implementation NSString (Extension)
- (CGSize)textSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
{
CGSize textSize;
if ([Device isIOS7orLater]) {
if (CGSizeEqualToSize(size, CGSizeZero)) {
textSize = [self sizeWithAttributes:@{NSFontAttributeName:font}];
}
else{
NSStringDrawingOptions option = NSStringDrawingUsesLineFragmentOrigin;
//NSStringDrawingTruncatesLastVisibleLine如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。 如果指定了NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略 NSStringDrawingUsesFontLeading计算行高时使用行间距。(译者注:字体大小+行间距=行高)
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
CGRect rect = [self boundingRectWithSize:size
options:option
attributes:attributes
context:nil];
textSize = rect.size;
}
}
else{
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
if (CGSizeEqualToSize(size, CGSizeZero)) {
textSize =[self sizeWithFont:font];
}
else{
textSize =[self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
}
#endif
}
return textSize;
}
- (BOOL)isContainSubString:(NSString *)subString{
if([self rangeOfString:subString].location != NSNotFound)
{
return YES;
}
else
{
return NO;
}
}
- (BOOL)stringContainsEmoji
{
__block BOOL returnValue = NO;
[self enumerateSubstringsInRange:NSMakeRange(0, [self length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
if ([@"➋➌➍➎➏➐➑➒" isContainSubString:self]) {
returnValue = NO;
}
return returnValue;
}
//- (BOOL)isMobileNumber
//{
// NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
// NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
// return [phoneTest evaluateWithObject:self];
//}
- (BOOL)isMobileNumber
{
if (self.length != 11)
{
return NO;
}
return YES;
/**
* 手机号码:
* 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
* 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
* 联通号段: 130,131,132,155,156,185,186,145,176,1709
* 电信号段: 133,153,180,181,189,177,1700
*/
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
/**
* 中国移动:China Mobile
* 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
*/
NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
/**
* 中国联通:China Unicom
* 130,131,132,155,156,185,186,145,176,1709
*/
NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
/**
* 中国电信:China Telecom
* 133,153,180,181,189,177,1700
*/
NSString *CT = @"(^1(33|53|77|73||8[019])\\d{8}$)|(^1700\\d{7}$)";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:self] == YES)
|| ([regextestcm evaluateWithObject:self] == YES)
|| ([regextestct evaluateWithObject:self] == YES)
|| ([regextestcu evaluateWithObject:self] == YES))
{
return YES;
}
else
{
return NO;
}
}
- (BOOL)isWebUrlStr{
if (self.length == 0)
{
return NO;
}
NSString * urlStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@;#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@;#$%^&*+?:_/=<>]*)?)";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlStr];
if ([regextestmobile evaluateWithObject:self] == YES)
{
return YES;
}
else
{
return NO;
}
}
- (BOOL)isAvailableEmail
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
- (BOOL)isIdentityNumber{
NSString *phoneRegex = @"^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
NSPredicate *identityTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [identityTest evaluateWithObject:self];
}
- (BOOL)isNumText{
//判断是不是纯数字
[NSCharacterSet decimalDigitCharacterSet];
if ([[self stringByTrimmingCharactersInSet: [NSCharacterSet decimalDigitCharacterSet]] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]].length >0) {
return NO;
}else{
return YES;
}
}
+(NSString *)returnActualFormatNumber:(double)number
{
// NSString *thirdNum = [NSString stringWithFormat:@"%0.3f",number];
// number = [thirdNum doubleValue];
//最多保留两位小数
number = round(number*100)/100;
NSArray *numArray =[[NSString stringWithFormat:@"%.2f",number] componentsSeparatedByString:@"."];
NSString *firstNum =[numArray firstObject];
NSString *secondNum =[numArray lastObject];
if ([secondNum doubleValue] == 0) {
return firstNum;
}
else
{
if ([[secondNum substringFromIndex:1] doubleValue] != 0) {
return [NSString stringWithFormat:@"%.2f",number];
}
else{
return [NSString stringWithFormat:@"%.1f",number];
}
}
return nil;
}
+(NSString *)returnDetailFormatNumber:(double)number leaveCount:(NSInteger)count{
number = round(number*100)/100;
NSArray *numArray =[[NSString stringWithFormat:@"%@",@(number)] componentsSeparatedByString:@"."];
NSString *firstNum =[numArray firstObject];
NSString *secondNum =[numArray lastObject];
if ([secondNum doubleValue] == 0) {
return firstNum;
}
else{
NSString *subSecondStr = [secondNum substringToIndex:count];
return [NSString stringWithFormat:@"%@.%@",firstNum,subSecondStr];
}
return nil;
}
+ (NSMutableAttributedString *)setNbLabelTextProperty:(NSString *)text
string:(NSString *)string
textColor:(UIColor *)color
textFont:(UIFont *)font
{
NSRange range;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
if (string != nil && ![string isEqualToString:@""]) {
range = [text rangeOfString:string];
}else{
range = NSMakeRange(0, 0);
}
[str addAttribute:NSForegroundColorAttributeName value:color range:range];
[str addAttribute:NSFontAttributeName value:font range:range];
return str;
}
+ (NSMutableAttributedString *)attributeStringWithText:(NSString *)text withLineSpace:(CGFloat )lineSpace withLineBreakMode:(NSLineBreakMode )lineBreakMode withFont:(UIFont *)font
{
NSMutableParagraphStyle *paragraphStyle =[[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = lineSpace;
paragraphStyle.lineBreakMode = lineBreakMode;
NSMutableAttributedString *attributeStr =[[NSMutableAttributedString alloc]initWithString:text
attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle}];
return attributeStr;
}
+ (NSMutableAttributedString *)attributebLabelText:(NSString *)text
withTextColor:(UIColor *)textColor
withLineSpace:(CGFloat)lineSpace
withTextFont:(UIFont *)textFont
withExtraStringArray:(NSArray *)extraStringArr
withExtraColorArray:(NSArray *)extraColorArr
withExtraFontArray:(NSArray *)extraFont;
{
if (!IsStrEmpty(text)) {
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
[attributeStr addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, text.length)];
[attributeStr addAttribute:NSFontAttributeName value:textFont range:NSMakeRange(0, text.length)];
NSMutableParagraphStyle * paragraphStyle =[[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
[paragraphStyle setLineSpacing:lineSpace];
[attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
for (int i=0; i< extraStringArr.count; i++) {
NSString * str = extraStringArr[i];
NSRange range = [text rangeOfString:str];
[attributeStr addAttribute:NSForegroundColorAttributeName value:extraColorArr[i] range:range];
[attributeStr addAttribute:NSFontAttributeName value:extraFont[i] range:range];
}
return attributeStr;
}
return nil;
}
+ (BOOL)validateIdentityCard:(NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:identityCard];
}
- (BOOL)isHaveSpaceInString
{
NSRange _range = [self rangeOfString:@" "];
if (_range.location != NSNotFound) {
return YES;
}else {
return NO;
}
}
- (BOOL)isHaveChineseInString
{
for(NSInteger i = 0; i < [self length]; i++){
int a = [self characterAtIndex:i];
if (a > 0x4e00 && a < 0x9fff) {
return YES;
}
}
return NO;
}
@end