iOS 代码碎片

2021-09-17  本文已影响0人  JimmyL

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..<gateList.count {
            let gateChar = to8Bit(number: gateList[index] as NSString)
            let maskChar = to8Bit(number: maskList[index] as NSString)
            let ipChar = to8Bit(number: ipList[index] as NSString)
            for j in 0..<8 {
                let charIndex = gateChar.index(gateChar.startIndex, offsetBy: j)
                if maskChar[charIndex] == "1" {
                    if gateChar[charIndex] != ipChar[charIndex] {
                        return false
                    }
                }
            }
        }
        
        return true
    }

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、判断密码格式是否正确

- (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<NSString *> *)specialTextArray normalFont:(UIFont *)normalFont specialFont:(NSArray <UIFont *> *)specialFont normalColor:(UIColor *)normalColor specialColor:(NSArray<UIColor *> *)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;
}
上一篇下一篇

猜你喜欢

热点阅读