美文网首页iOS随笔小记
iOS随笔小记-- 判断手机号、字符、邮箱、根据标题的内容来获取

iOS随笔小记-- 判断手机号、字符、邮箱、根据标题的内容来获取

作者: 七一小月 | 来源:发表于2017-06-06 10:50 被阅读12次

    .h文件

    //如果符合邮箱格式,返回YES

    -(BOOL)isValidateEmail:(NSString *)email

    /**

    * 判断是不是手机号

    */

    +(BOOL)judegMobileByPhoneNumber:(NSString *)phoneText;

    /**

    * 判断字符串为6~12位“字符”

    */

    +(BOOL)isValidateName:(NSString *)name;

    /**

    * 判断字符串是否为空

    */

    +(BOOL) isBlankString:(NSString *)string;

    /**

    *  设置lable的行间距

    */

    +(NSMutableAttributedString *)setlineSpacing:(NSString *)content andLabel:(UILabel *)contentLabel andLineSpacing:(int)lineSpace andFirstLineHeadIndent:(int)firstLineHeadIndent;

    /**

    *  根据标题的内容来获取lable变高

    */

    +(CGFloat)getHight:(NSString *)text andFont:(UIFont *)font andWidth:(CGFloat)width;

    .m文件

    //如果符合邮箱格式,返回YES

    -(BOOL)isValidateEmail:(NSString *)email {

    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:email];

    }

    //判断是不是手机号

    +(BOOL)judegMobileByPhoneNumber:(NSString *)phoneText{

    /**

    * 移动号段正则表达式

    */

    NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";

    /**

    * 联通号段正则表达式

    */

    NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";

    /**

    * 电信号段正则表达式

    */

    NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";

    NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];

    BOOL isMatch1 = [pred1 evaluateWithObject:phoneText];

    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];

    BOOL isMatch2 = [pred2 evaluateWithObject:phoneText];

    NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];

    BOOL isMatch3 = [pred3 evaluateWithObject:phoneText];

    BOOL result = (isMatch1 || isMatch2 || isMatch3);

    return result;

    }

    //判断字符串为6~12位“字符”

    +(BOOL)isValidateName:(NSString *)name{

    NSUInteger  character = 0;

    for(int i=0; i< [name length];i++)

    {

    int a = [name characterAtIndex:i];

    if( a > 0x4e00 && a < 0x9fff)

    { //判断是否为中文

    character +=2;

    }

    else

    {

    character +=1;

    }

    }

    if (character >=6 && character <=20)

    {

    return YES;

    }

    else

    {

    return NO;

    }

    }

    //判断字符串是否为空

    +(BOOL)isBlankString:(NSString *)string{

    if (string == nil || string == NULL) {

    return YES;

    }

    if ([string isKindOfClass:[NSNull class]]) {

    return YES;

    }

    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {

    return YES;

    }

    return NO;

    }

    //设置lable的行间距

    + (NSMutableAttributedString *)setlineSpacing:(NSString *)content andLabel:(UILabel *)contentLabel andLineSpacing:(int)lineSpace andFirstLineHeadIndent:(int)firstLineHeadIndent{

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content];

    NSMutableParagraphStyle * paragraphStyle = [[ NSMutableParagraphStyle alloc] init ];

    paragraphStyle.maximumLineHeight = 30 ;  //最大的行高

    paragraphStyle.lineSpacing = lineSpace ;  //行自定义行高度

    [paragraphStyle setFirstLineHeadIndent:firstLineHeadIndent ]; //首行缩进

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range : NSMakeRange ( 0 , content.length)];

    return attributedString;

    }

    //根据内容来获取变高

    + (CGFloat)getHight:(NSString *)text andFont:(UIFont *)font andWidth:(CGFloat)width{

    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];

    return rect.size.height;

    }

    相关文章

      网友评论

        本文标题:iOS随笔小记-- 判断手机号、字符、邮箱、根据标题的内容来获取

        本文链接:https://www.haomeiwen.com/subject/oujgfxtx.html