NSString的分类

简要

平时我们在做项目时用到NSString的地方很多,本文对我们在做项目时最常用的做了总结,废话不多说直接上代码。

1.首先我们创建一个NSString的分类(category)

屏幕快照 2019-11-28 下午2.01.01.png

2.在.h中写入

#import 

NS_ASSUME_NONNULL_BEGIN

@interface NSString (LCString)
/**
 *  计算字符串内容大小
 *
 *  @param font    字体
 *  @param maxSize 最大范围
 *
 *  @return 内容大小
 */
- (CGSize)sizeWithTextfont:(UIFont *)font maxSize:(CGSize)maxSize;
/**
 *  判断是否为手机号码
 *
 *  @return YES或NO
 */
- (BOOL)validatePhoneNumber;

/**
 判断是否为邮箱
 
 @return YES或NO
 */
- (BOOL)validateMailInput;
/**
 判断身份证号
 
 @return YES或NO
 */
- (BOOL)validateIdentityCard;

/**
 判断银行卡号
 
 @return YES或NO
 */
- (BOOL)validateBankCard;
/**
 判断密码位数
 
 @return YES或NO
 */
- (BOOL)validatePasswordAmount;

/**
 检查密码 (英文/数字/符号)组合
 
 @return YES或NO
 */
- (BOOL)validatePasswordMakeUp;

/**
 检查是不是纯数字
 
 @return YES或NO
 */
- (BOOL)validateTextIsNumber;

/**
 正则去除标签
 
 @return 字符串
 */
- (NSString *)detachWithTag;

/**
 去掉空字符串
 
 @return 字符串
 */
- (NSString *)detachWithEmptySting;

/**
 手机号加****
 
 @return  手机号加****
 */
- (NSString *)hidePhoneNumebr;
@end

NS_ASSUME_NONNULL_END

3.在.m中写入

#import "NSString+LCString.h"

@implementation NSString (LCString)
//获取文本高度
- (CGSize)sizeWithTextfont:(UIFont *)font maxSize:(CGSize)maxSize{
    NSDictionary *attrs = @{NSFontAttributeName : (font?font:[UIFont systemFontOfSize:14])};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

/**
 *  判断是否为手机号码
 *
 *  @return YES或NO
 */
- (BOOL)validatePhoneNumber {
    
    NSString * mobile = @"^1[3456789]\\d{9}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobile];
    if ([regextestmobile evaluateWithObject:self] == YES) {
        return YES;
    }else {
        return NO;
    }
}
/**
 判断是否为邮箱
 
 @return YES或NO
 */
- (BOOL)validateMailInput {
    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];
}
/**
 判断身份证号
 
 @return YES或NO
 */
- (BOOL)validateIdentityCard {
    if (self.length <= 0) {
        return NO;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:self];
}
/**
 判断银行卡号
 
 @return YES或NO
 */
- (BOOL)validateBankCard {
    if(self.length==0)
    {
        return NO;
    }
    NSString *digitsOnly = @"";
    char c;
    for (int i = 0; i < self.length; i++)
    {
        c = [self characterAtIndex:i];
        if (isdigit(c))
        {
            digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
        }
    }
    int sum = 0;
    int digit = 0;
    int addend = 0;
    BOOL timesTwo = false;
    for (NSInteger i = digitsOnly.length - 1; i >= 0; i--)
    {
        digit = [digitsOnly characterAtIndex:i] - '0';
        if (timesTwo)
        {
            addend = digit * 2;
            if (addend > 9) {
                addend -= 9;
            }
        }
        else {
            addend = digit;
        }
        sum += addend;
        timesTwo = !timesTwo;
    }
    int modulus = sum % 10;
    return modulus == 0;
}
/**
 判断密码位数
 
 @return YES或NO
 */
- (BOOL)validatePasswordAmount {
    NSString *emailRegex = @"^[\\S]{6,16}$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return ![emailTest evaluateWithObject:self];
}
/**
 检查密码 (英文/数字/符号)组合
 
 @return YES或NO
 */
- (BOOL)validatePasswordMakeUp {
    NSString *regex = @"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:self];
}
/**
 检查是不是纯数字
 
 @return YES或NO
 */
- (BOOL)validateTextIsNumber {
    NSString *value = self;
    value = [self stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(value.length > 0) {
        return NO;
    }
    return YES;
}

/**
 正则去除标签
 
 @return 字符串
 */
- (NSString *)detachWithTag {
    NSString *string = self;
    NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n" options:0 error:nil];
    string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
    return string;
}
/**
 去掉空字符串
 
 @return 字符串
 */
- (NSString *)detachWithEmptySting {
    
    NSString*str = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    return str;
}
/**
 手机号加****
 
 @return  手机号加****
 */
- (NSString *)hidePhoneNumebr {
    NSString *newPhone = self;
    if (self && self.length > 5) {
        // 用户手机、企业手机
        newPhone = [self stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    }
    return newPhone;
}
@end

你可能感兴趣的:(NSString的分类)