UITextField相关问题

 1.  判断输入的是否全为数字

  NSScanner* scan = [NSScannerscannerWithString:self.countTextField.text];

        int val;

        NSString *string = [NSStringstringWithFormat:@"%d",[scanscanInt:&val] && [scan isAtEnd]];

        NSLog(@"%d",[scanscanInt:&val] && [scanisAtEnd]);

        if ([string isEqualToString:@"0"]) {

            // 0代表不全为数字

        }else {

            // 1代表输入的全为数字

        }

 

2. 修改UITextField中placeholder的字体颜色

(1)

 

NSMutableDictionary *dict = [NSMutableDictionarydictionary];

    dict[NSForegroundColorAttributeName] =Color(201,197,189);

    NSAttributedString *attribute = [[NSAttributedStringalloc]initWithString:self.emailText.placeholderattributes:dict];

    [self.emailTextsetAttributedPlaceholder:attribute];

(2)有了placeholderLabel.textColor这个属性,在storyboard/xib中利用KVC设置placeholder颜色,如图

 

3. textField 不顶格输入(自定义的textField总是顶格输入,是否影响美观呢?是否曾用空格解决过这个问题?)

    tel.leftView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,8,0)];

    tel.leftViewMode =UITextFieldViewModeAlways;

4. textField 密文输入

 

    password.secureTextEntry =YES;

5. 限制texfield输入位数(例如以下,最多输入4位)

 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    if (textField == self.authorizationPassword) {

        if (string.length ==0) returnYES;

        

        NSInteger existedLength = textField.text.length;

        NSInteger selectedLength = range.length;

        NSInteger replaceLength = string.length;

        if (existedLength - selectedLength + replaceLength >4) {

            return NO;

        }

    }

    return YES;

}

6.UITextField无法改变高度处理

在IB中直接拉一个TextField到xib中,调整高度

发现高度直接锁定一个数字,灰着,不能修改,解决方法:

(1)ib里面就是锁定的
   更改textField的border样式,就可以在ib更改高度

(2)在代码里面更改高度

   在viewIdLoad方法加上

    CGRect rect = txtNum.frame;
    rect.size.height = 50;
    txtNum.frame = rect;

7.控制textField是否可被编辑

 

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

    if ([self.canEditingisEqualToString:@"1"]) {

        return YES;

    }

    return NO;

}

8. iOS改变UITextField中光标颜色

 

 

 

//    _textField.tintColor = [UIColor redColor]; 改变光标的颜色    同时复制粘贴的颜色也变了  然后我想到了下面的情况

下面如果你想要禁用复制粘贴的功能  

如果想禁止复制粘贴功能那么就重写

 

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

    if (action == @selector(cut:)){

        return NO;

    }

    else if(action == @selector(copy:)){

        return YES;

    }

    else if(action == @selector(paste:)){

        return NO;

    }

    else if(action == @selector(select:)){

        return NO;

    }

    else if(action == @selector(selectAll:)){

        return NO;

    }

    else

    {

        return [super canPerformAction:action withSender:sender];

    }

}

9.设置字符间间距

    NSDictionary *attrsDictionary =@{

                                     NSFontAttributeName: self.textField.font,

                                     NSKernAttributeName:[NSNumber numberWithFloat:30.0f]//这里修改字符间距

                                     };

    self.textField.attributedText=[[NSAttributedString alloc]initWithString:self.textField.text attributes:attrsDictionary];

10.隐藏键盘上方提示区域

_usernameTextField.inputAccessoryView = [[UIView alloc] init];
 


 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值