UITextFiled使用

 textField.text = [textField.text substringToIndex:40];

只输入数字

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
   BOOL res = YES;
   NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
   int i = 0;
   while (i < number.length) {
       NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
       NSRange range = [string rangeOfCharacterFromSet:tmpSet];
       if (range.length == 0) {
           res = NO;
           break;
       }
       i++;
   }
   return res;
}

_txtF = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.width, 30)];
_txtF.font = KFONT_14;
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入内容" attributes:
    @{NSForegroundColorAttributeName:[UIColor redColor],
                    NSFontAttributeName:_txtF.font
    }];
_txtF.attributedPlaceholder = attrString;
[self.view addSubview:_txtF];

禁止输入汉字

说明:

^.*[\u4e00-\u9fa5].*$ 是否包含中文
^[\u4E00-\u9FA5]+$ 是否全中文

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (![string isEqualToString:@""]) {
if ([self isChinese:string]) {
return NO;
}
}
return YES;
}


- (BOOL)isChinese:(NSString *)string {
NSString *regex = @"^[\u4E00-\u9FA5]+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

BOOL isMatch = [pred evaluateWithObject:string];
return isMatch;
}

禁止编辑

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

if (textfield.tag==??) {

return NO;

}

return YES;

}


 

编辑状态

 让UITextField直接进入编辑状态

[TextField becomeFirstResponder];


让UITextField失去第一响应

[TextField resignFirstResponder];

1.placeholder居中

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentCenter;
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:@"输入房间名称" attributes:@{NSForegroundColorAttributeName:RGB(163, 163, 163),NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:style}];
    _nameField.attributedPlaceholder = attri;

2.显示删除键

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

3.return 键收起键盘

方法一

  field.returnKeyType = UIReturnKeyDone;
  field.delegate = self;

需要实现代理:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    switch (textField.returnKeyType) {
        // 键盘为done的Case
        case UIReturnKeyDone:
            [textField resignFirstResponder];
            break;
        default:
            break;
    }
    return YES;
}

方法二

#pragma mark - 监听View点击事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    // 如果点击到UITextField以外的View则收回键盘
    if (![touch.view isKindOfClass:[UITextField class]]) {
        [self.view endEditing:YES];
    }
}

4.暗文

tf.secureTextEntry = YES;

5.汉字限制

^.*[\u4e00-\u9fa5].*$ 是否包含中文
^[\u4E00-\u9FA5]+$ 是否全中文

- (BOOL)isChinese:(NSString *)string {
NSString *regex = @"^[\u4E00-\u9FA5]+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

BOOL isMatch = [pred evaluateWithObject:string];
return isMatch;
}

6.监听输入变化
[textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
- (void)textFieldChanged:(UITextView *)textField{
}

7.placeholder颜色

NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:

    @{NSForegroundColorAttributeName:[UIColor redColor],

                NSFontAttributeName:textField.font        }];

    textField.attributedPlaceholder = attrString;

8.return按钮设置

searchText.returnKeyType=UIReturnKeySearch;

searchText.delegate=self;

-(BOOL)textFieldShouldReturn:(UITextField*)textField{

    returnYES;

}


 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值