iOS开发之常用分类

本文介绍了常用的一些分类.

一.UIColor分类(十六进制转颜色)
/**
 十六进制转颜色

 @param hexStr 十六进制字符串
 @return 颜色对象
 */
+ (instancetype)colorWithHexStr:(NSString *)hexStr
{
    if ([hexStr hasPrefix:@"#"])
    {
        hexStr = [hexStr stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
    }
    NSInteger redColorNum = strtoul([[hexStr substringWithRange:NSMakeRange(0, 2)] UTF8String],0,16);
    NSInteger greenColorNum = strtoul([[hexStr substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16);
    NSInteger blueColorNum = strtoul([[hexStr substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16);
    UIColor *color = BYColor(redColorNum, greenColorNum, blueColorNum);
    return color;
}
二.UITextField分类(手机账号和密码验证)
- (BOOL)isEmptyText
{
    return self.text.length == 0;
}

/*
 中国移动:
 135、136、137、138、139、147(数据卡)、148、150、151、152、157、158、159、178、182、183、184、187、188、198、
 134(0-8)、1440、1703、1705、1706

 中国联通:
 130、131、132、145(数据卡)、146、155、156、166、175、176、185、186、
 1707、1708、1709 、

 中国电信:
 133、149、153、173、177、180、181、189、199、
 1349、1410、1700、1701、1702、

 虛拟运营商: 1700、1705、1707、1708、1709......代理联通移动电信业务

 */
// 判断手机号码格式是否正确
- (BOOL)isValidPhoneNumber
{
    //    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (self.text.length != 11)
    {
        return NO;
    }
    else
    {
        /**
         * 移动号段正则表达式
         * CM China Mobile
         */
        NSString *CMPhoneSegmentRegExp = @"^1((3[5-9])|(4[7-8])|(5[0-2,7-9])|(78)|(8[2-4,7-8])|(98))\\d{8}|((34[0-8])|(440)|(70[3,5-6]))\\d{7}$";
        /**
         * 联通号段正则表达式
         * CU China Unicom
         */
        NSString *CUPhoneSegmentRegExp = @"^1((13[0-2])|(4[5-6])|(5[5-6])|(66])|(7[5-6])|(8[5,6]))\\d{8}|(70[7-9])\\d{7}$";
        /**
         * 电信号段正则表达式
         * CT China Telecom
         */
        NSString *CTPhoneSegmentRegExp = @"^1((33)|(49)|(53)|(17[3-7])|(18[0,1,9])|(99))\\d{8}((349)|(410)|(70[0-2]))\\d{7}$";

        NSPredicate *predicateCM = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CMPhoneSegmentRegExp];
        BOOL isMatchCM = [predicateCM evaluateWithObject:self.text];
        NSPredicate *predicateCU = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CUPhoneSegmentRegExp];
        BOOL isMatchCU = [predicateCU evaluateWithObject:self.text];
        NSPredicate *predicateCT = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CTPhoneSegmentRegExp];
        BOOL isMatchCT = [predicateCT evaluateWithObject:self.text];

        if (isMatchCM || isMatchCU || isMatchCT)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
}

// 判断秘密格式为 6 - 18 位数字和字母组成
- (BOOL)isValidPassword
{
    NSString *regExp = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regExp];
    if ([predicate evaluateWithObject:self.text])
    {
        return YES ;
    }
    else
    {
        return NO;
    }
}

// 长度是否超过6位数并小于18位数
- (BOOL)isMoreThanSixLength
{
    return (self.text.length >= 6 && self.text.length <= 18);
}
三.NSString分类(缓存路径)
// 获取Documents目录
+ (NSString *)documentPath
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

// 获取Cache目录
+ (NSString *)cachePath
{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}

// 获取Tmp目录 
+ (NSString *)tempPath
{
    return NSTemporaryDirectory();
}

// 获取拼接后的Documents目录
- (NSString *)appendDocumentsPath
{
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = [self lastPathComponent];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];

    return filePath;
}

// 获取拼接后的Cache目录
- (NSString *)appendCachePath
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = [self lastPathComponent];
    NSString *filePath = [cachePath stringByAppendingPathComponent:fileName];

    return filePath;
}

// 获取拼接后的Tmp目录
- (NSString *)appendTmpPath
{
    NSString *tmpPath = NSTemporaryDirectory();
    NSString *fileName = [self lastPathComponent];
    NSString *filePath = [tmpPath stringByAppendingPathComponent:fileName];

    return filePath;
}

后续再更…

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值