美文网首页
iOS - 输入框

iOS - 输入框

作者: CDLOG | 来源:发表于2018-08-17 14:25 被阅读400次

textField

指针闪动颜色

textField.tintColor

设置左间距

    [textField.layer setMasksToBounds:YES];
    [textField.layer setCornerRadius:5.0];
    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
    textField.leftViewMode = UITextFieldViewModeAlways;

清除输入框内容

self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;//出现一个差号用于清除输入的内容
    /*
     UITextFieldViewModeNever,不出现
     UITextFieldViewModeWhileEditing,当编辑的时候出现差号
     UITextFieldViewModeUnlessEditing,当不对其编辑的时候出现
     UITextFieldViewModeAlways总是出现
*/

输入框高度

- (CGSize)setMultiLineWithLable:(UILabel *)lable
{
    
    lable.font = [UIFont systemFontOfSize:14];
    lable.text = self.priceModel.content;

    lable.numberOfLines = 0;//多行显示,计算高度
    lable.textColor = color_light_gray;
    CGSize size  = [lable.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    lable.frame = CGRectMake(lable.frame.origin.x, lable.frame.origin.y, size.width, size.height);
    return size;
}

输入框只能输入数字和长度

#pragma mark - textFieldDelegate
//长度判断

//先使输入有效再判断

NSString * aStr = [textField.text stringByReplacingCharactersInRange:range withString:string];


#pragma mark - textFieldDelegate
//长度判断
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger maxLength = 11;
    if(string.length == 0)
        return YES;
        NSString * aStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(aStr.length > maxLength)
            return NO;
    
    return [self isInputRuleAndNumber:aStr andTextField:textField];
}
//字符判断r
#pragma mark - 谓词条件限制
/**不同的field,支持不同的字符
 pattern中,输入需要验证的通过的字符
 小写a-z
 大写A-Z
 汉字\u4E00-\u9FA5
 数字\\u0030-\\u0039
 @param str 要过滤的字符
 @return YES 只允许输入字母和汉字
 */

- (BOOL)isInputRuleAndNumber:(NSString *)str andTextField:(UITextField *)field{
    NSString *pattern = [[NSString alloc]init];
    if ([field isEqual:self.thirdAccountField]) {
        pattern = @"[\u4E00-\u9FA5]*";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
        BOOL isMatch = [pred evaluateWithObject:str];
        if ([self isSudoku:str] || isMatch) {
            return YES;
        }
    }else{
        pattern = @"[\\u0030-\\u0039]*";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
        BOOL isMatch = [pred evaluateWithObject:str];
        if (isMatch) {
            return YES;
        }
    }
   
    
    
    return NO;
}
-(BOOL)isSudoku:(NSString *)str{
    NSString *other = @"➋➌➍➎➏➐➑➒";
    NSString *lastStr = [str substringFromIndex:str.length-1];
    if (![other containsString:lastStr]) {
        return NO;
    }
    
    unsigned long len=lastStr.length;
    for(int i=0;i<len;i++)
    {
        unichar a=[lastStr characterAtIndex:i];
        if(!((isalpha(a))
             ||(isalnum(a))
             ||((a=='_') || (a == '-'))
             ||((a >= 0x4e00 && a <= 0x9fa6))
             ||([other rangeOfString:lastStr].location != NSNotFound)
             ))
            return NO;
    }
    return YES;
}

有小数点的数字键盘

self.accountField.keyboardType = UIKeyboardTypeDecimalPad;

textView

有输入时隐藏提示的lable

判断输入的值是否有效,先让他有效再判断是否隐藏)

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString * str = [self.carInfoTextView.text stringByReplacingCharactersInRange:range withString:text];
    if(str.length>0){
        self.hintLable.hidden = YES;
    }else{
        self.hintLable.hidden = NO;
    }
    return YES;
}

或者

#pragma mark - textViewDelegate
-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.bankPlaceHoder.hidden = YES;
}
-(void)textViewDidChange:(UITextView *)textView{
    if ([@"" isEqualToString: textView.text ]) {
        self.bankPlaceHoder.hidden = NO;
    }else{
        self.bankPlaceHoder.hidden = YES;
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if ([@"" isEqualToString: textView.text ]) {
        self.bankPlaceHoder.hidden = NO;
    }else{
        self.bankPlaceHoder.hidden = YES;
    }
}

字数提示

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    
    NSString * str = [self.carInfoTextView.text stringByReplacingCharactersInRange:range withString:text];
    if(str.length>0){
        self.hintLable.hidden = YES;
    }else{
        self.hintLable.hidden = NO;
    }
    
    NSInteger maxLength = 200;
    if ( textView== self.carInfoTextView) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && text.length == 0) {
            return YES;
        }
        //so easy
        else if (self.carInfoTextView.text.length >= maxLength) {
            self.carInfoTextView.text = [textView.text substringToIndex:maxLength];
            return NO;
        }
    }
    NSString * length = [NSString stringWithFormat:@"%ld/%ld",str.length,maxLength];
    self.textLenthLable.text = length;
    
    return YES;
}

相关文章

网友评论

      本文标题:iOS - 输入框

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