美文网首页
iOS使用系统数字键盘闪退

iOS使用系统数字键盘闪退

作者: 曾经像素有点低 | 来源:发表于2022-10-17 18:22 被阅读0次
    iOS使用系统数字键盘闪退 UIKeyboardTypeNumberPad

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM __setObject:forKey:]: object cannot be nil (key: NSFont)'

    1.代码如下:
        UITextField *fieldText = [[UITextField alloc] initWithFrame:CGRectMake( 30, 120, 200, 25)];
        fieldText.placeholder = @"请输入手机号";
        fieldText.keyboardType  = UIKeyboardTypeNumberPad;
        fieldText.clearButtonMode = UITextFieldViewModeWhileEditing;
        fieldText.font = [UIFont systemFontOfSize:16];
        [self.view addSubview: fieldText];
    
    2.报错如下:
    Terminating app due to uncaught exception
     'NSInvalidArgumentException', 
    reason: '*** -[__NSDictionaryM __setObject:forKey:]: 
    object cannot be nil (key: NSFont)'
    
    3.描述

    将键盘模式设置成:
    UIKeyboardTypeDefault
    UIKeyboardTypeNumbersAndPunctuation
    都可以使用,但是使用UIKeyboardTypeNumberPad闪退。
    (按键1、2、3..闪退,按键0不闪退)
    报错信息啥也看不出来。。。。

    4.解决问题:

    后来发现是项目中有一个UIFont+ETExtension的扩展类
    ,里边使用了方法交换。具体问题暂时没有发现。但是注释了方法交换后就没事儿了。还在继续钻研中。

    + (void)swizzleFontMethod {
        
        if (isIOS11) {
            // 交换systemFont
            Method originalMethod = class_getClassMethod(self, @selector(systemFontOfSize:));
            Method swizzledMethod = class_getClassMethod(self, @selector(et_systemFontOfSize:));
            method_exchangeImplementations(originalMethod, swizzledMethod);
            
            // 交换boldFont
            originalMethod = class_getClassMethod(self, @selector(boldSystemFontOfSize:));
            swizzledMethod = class_getClassMethod(self, @selector(et_boldFontOfSize:));
            method_exchangeImplementations(originalMethod, swizzledMethod);
            
            // 交换systemFontOfSize:weight:
            originalMethod = class_getClassMethod(self, @selector(systemFontOfSize:weight:));
            swizzledMethod = class_getClassMethod(self, @selector(et_systemFontOfSize:weight:));
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
       
    }
    
    + (UIFont *)et_systemFontOfSize:(CGFloat)fontSize {
        return [UIFont fontWithName:PingFang_SC_Regular size:fontSize];
    }
    
    + (UIFont *)et_boldFontOfSize:(CGFloat)fontSize {
        return [UIFont fontWithName:PingFang_SC_Bold size:fontSize];
    }
    
    + (UIFont *)et_systemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight {
        // 根据weight 替换为 PingFang_SC字体的weight
        UIFont *font = [UIFont fontWithName:PingFang_SC_Regular size:fontSize];
        if (weight == UIFontWeightThin) {
            font = [UIFont fontWithName:PingFang_SC_Thin size:fontSize];
        } else if (weight == UIFontWeightLight) {
            font = [UIFont fontWithName:PingFang_SC_Light size:fontSize];
        } else if (weight == UIFontWeightMedium) {
            font = [UIFont fontWithName:PingFang_SC_Medium size:fontSize];
        } else if (weight == UIFontWeightSemibold) {
            font = [UIFont fontWithName:PingFang_SC_Semibold size:fontSize];
        } else if (weight == UIFontWeightBold) {
            font = [UIFont fontWithName:PingFang_SC_Bold size:fontSize];
        } else if (weight == UIFontWeightUltraLight) {
            font = [UIFont fontWithName:PingFang_SC_Ultralight size:fontSize];
        }
        return font;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS使用系统数字键盘闪退

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