iOS 代码碎片

1、通过掩码、网关判断ip是否有效

fileprivate func verfyIpAddressEnable(gateway: String, netmask: String, ipAddr: String) -> Bool {
        func to8Bit(number: NSString) -> String {
            var result = String.init(number.integerValue, radix: 2, uppercase: false)
            while result.count < 8 {
                result = "0" + result
            }
            return result
        }

        let gateList = gateway.split(separator: ".")
        let maskList = netmask.split(separator: ".")
        let ipList = ipAddr.split(separator: ".")
        for index in 0..

2、判断两个数组是否相等,顺序不考虑

- (BOOL)isTwoArrayEqualWithOneArray:(NSArray *)oneArray otherArray:(NSArray *)otherArray {
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", oneArray];
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", otherArray];
    if (([otherArray filteredArrayUsingPredicate:predicate1].count > 0) || ([oneArray filteredArrayUsingPredicate:predicate2].count > 0)) {
        return NO;
    }
    return YES;
}

3、判断密码格式是否正确

  • 不能全部是数字
  • 不能全部是字母
  • 必须是数字和字母组合
  • 长度6-16位
- (BOOL)isPasswordCorrectType:(NSString *)string {
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:string];
}

4、设置富文本组合文案

+ (NSAttributedString *)setAttributeStringWithFullText:(NSString *)fullText specialText:(NSArray *)specialTextArray normalFont:(UIFont *)normalFont specialFont:(NSArray  *)specialFont normalColor:(UIColor *)normalColor specialColor:(NSArray *)specialColor {
    
    NSMutableAttributedString *suctionString = [[NSMutableAttributedString alloc]initWithString:fullText attributes:@{
                                                                                                                      NSFontAttributeName:normalFont,
                                                                                                                      NSForegroundColorAttributeName:normalColor
                                                                                                                      }];
    [specialTextArray enumerateObjectsUsingBlock:^(NSString * _Nonnull specialText, NSUInteger idx, BOOL * _Nonnull stop) {
        if (specialText.length > 0) {
            
            NSRange range1=[[suctionString string]rangeOfString:specialText];
            if (specialFont[idx]) {
                [suctionString addAttribute:NSFontAttributeName value:specialFont[idx] range:range1];
            }
            if (specialColor[idx]) {
                [suctionString addAttribute:NSForegroundColorAttributeName value:specialColor[idx] range:range1];
            }
        }
    }];
    return suctionString;
}

5、输入框clearButton图片替换

- (void)changeClearButtonTintImageWithTextField:(UITextField *)textField {
    UIButton *clearButton = (UIButton *)[textField valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"clearButton"] forState:UIControlStateNormal];
    [clearButton setImage:[UIImage imageNamed:@"clearButton"] forState:UIControlStateHighlighted];
}

6、不可粘贴复制的输入框

新建 UITextField 子类,覆写 -(BOOL)canPerformAction:withSender:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    //安全输入模式不能剪切、复制
    if (self.secureTextEntry) {
        if (action == @selector(copy:) || action == @selector(cut:)) {
            return  NO;
        }
    }
    return YES;
}

你可能感兴趣的:(iOS 代码碎片)