iOS - 判断手机号、字符、邮箱、根据标题的内容来获取lable变高等

1. 在.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;

2.在.m文件实现h文件中的方法

a.如果符合邮箱格式,返回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];

}

b.判断是不是手机号

+ (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;

}

c.判断字符串为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;

        }

}

d.判断字符串是否为空

+ (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;

}

e.设置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;

}

f.根据内容来获取变高

+ (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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容