一同养成写作习气!这是我参加「日新方案 4 月更文挑战」的第20天,点击检查活动详情。

I UITextField的使用小技巧

1.1 设置键盘的返回文字信息为查找类型

    _searchTf.returnKeyType = UIReturnKeySearch;//变为查找按钮

iOS小技能:UITextField的使用
returnKeyType

 typedef NS_ENUM(NSInteger, UIReturnKeyType) {
 UIReturnKeyDefault,
 UIReturnKeyGo,//去往
 UIReturnKeyGoogle,
 UIReturnKeyJoin,//参加
 UIReturnKeyNext,//下一步
 UIReturnKeyRoute,
 UIReturnKeySearch,//查找
 UIReturnKeySend,//发送
 UIReturnKeyYahoo,
 UIReturnKeyDone,//完成
 UIReturnKeyEmergencyCall,
 UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),};

当然,要显示中文, 还得设置info.plist中Localization native development region 为 cn

1.2 监控失去焦点


// 监控失去焦点
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //    _descLabel.text = @"失去焦点";
    return;//暂时不处理
    NSLog(@"失去焦点");
    if(_searchTf == textField ){
        //请求接口
        if(_searchTf.text.length>0){
        }
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //请求接口
    NSLog(@"点击了查找");
    if(self.searchTfblock){
        self.searchTfblock(textField);
    }
    return YES;
}

1.3 监听文本铲除

应用场景:登录界面铲除账号文本框内容的时分,告诉父控件去铲除暗码框的内容

    self.textF.clearButtonMode = models.clearButtonMode;
    if (models.clearButtonMode == UITextFieldViewModeAlways) {
        UIButton *clearButton = [self.textF valueForKey:@"_clearButton"];
        [[clearButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            NSLog(@"");
        }];
        [clearButton setImage:[UIImage imageNamed:@"icon_close"] forState:UIControlStateNormal];
        WS(weakSelf);
        [[clearButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
            //登录界面铲除账号文本框内容的时分,告诉父控件去铲除暗码框的内容
            if (weakSelf.models.clearButtonblock) {
                weakSelf.models.clearButtonblock(x);
            }
        }];
    }

II 约束UITextField的输入格局

2.1 约束只能输入金额的正则表达式

#pragma mark - ******** 约束只能输入金额的正则表达式
/**
  约束只能输入金额的正则表达式 :0.01
 @param str     NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string];
 @return <#return value description#>
 */
+ (BOOL)isAmoutshouldChangeCharactersInRange:(NSString*)str{
    //匹配以0开头的数字
    NSPredicate * predicate0 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[0][0-9]+$"];
    //匹配两位小数、整数
    NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^(([1-9]{1}[0-9]*|[0])\.?[0-9]{0,2})$"];
    return ![predicate0 evaluateWithObject:str] && [predicate1 evaluateWithObject:str] ? YES : NO;
}

约束UITextField只能输入金额的正则表达式


/**
 约束UITextField只能输入金额的正则表达式
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextFieldTextDidChangeNotification object:moneyTextField];
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSInteger len = range.length > 0?([textField.text length] - range.length): ([textField.text length] + [string length]);
    NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string];
    if(self.models.shouldChangeCharactersInRangeblock){
        return self.models.shouldChangeCharactersInRangeblock(str);
    }
    switch (self.models.EnterModelType) {
        case QCTUserEnterModelType4mobileNo:
        case QCTUserEnterModelType4EditDeliveryPhone:
        {
                    if (range.location > 10) {
                        return NO;
                    }else{
                                return YES;
                    }
        }
            break;
        case QCTUserEnterModelType4EditGoodPrice:
            {
                // 金额格局
                NSInteger len = range.length > 0?([textField.text length] - range.length): ([textField.text length] + [string length]);
                if(len > 20){
                    return false;
                }
                ////
                NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string];
                return [QCT_Common isAmoutshouldChangeCharactersInRange:str];
            }
            break;
        default:
            return true;
            break;
    }
}

2.2 约束文本输入框输入特定字符个数

文本长度计算规矩:中文占1,英文等能转ascii的占0.5

  1. 特色功用: iOS约束联想字符串输入,汉字占2个字符,英文占1个字符,处理的中英文切换输入法联想导致的bug
  2. demo需求:风险商户处理界面提供情况说明输入框,约束输入100个字符
  3. demo项目选用MVVM进行实现

iOS小技能:UITextField的使用

从CSDN下载demo :https://download.csdn.net/download/u011018979/15842447