iOS textField的一些奇怪问题

1.创建一个textField,字数限制20,去掉收尾空格

遇到的问题: iOS9以下,中文输入的情况下,点击联想字输入是不会回调

在iOS11上如果不设置textField的边框,它的显示会有问题,暂时未找到问题产生的原因,先这样处理吧

//输入文本框
    _textField.font = [UIFont systemFontOfSize:16];
    _textField.textColor = UIColorFromRGB(0x111111);
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.textAlignment = NSTextAlignmentLeft;
    _textField.returnKeyType = UIReturnKeyDone;
    _textField.delegate = self;
    _textField.placeholder = @"请输入分类名称";
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;//后面的删除小按钮
    _textField.layer.borderColor = [UIColor whiteColor].CGColor;
    _textField.layer.borderWidth = 1.0;
    _textField.layer.masksToBounds = YES;
    if (!IOS9) {
        [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    
    //字数监控
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(0), @(kAgendaLabelMaxLength)];
    _pageLabel.textAlignment = NSTextAlignmentRight;
    _pageLabel.font = [UIFont systemFontOfSize:13];
    _pageLabel.backgroundColor = [UIColor clearColor];
    _pageLabel.textColor = UIColorFromRGB(0x999999);

 

2.实现协议

#pragma mark - UITextFieldDelegate

-(BOOL)textFieldShouldClear:(UITextField *)textField {
    textField.text = @"";
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(0), @(20)];return NO;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSString *originString = textField.text;
    NSUInteger newCount = [originString length];
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(MIN(newCount, 20)), @(20)];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSString *originString = textField.text;
    NSUInteger newCount = [originString length];
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(MIN(newCount, 20)), @(20)];
}

- (void)textFieldDidChange:(UITextField *)textField {
    NSString *originString = textField.text;
    NSUInteger newCount = [originString length];
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(MIN(newCount, 20)), @(20)];if(textField.text.length > 20 && textField.markedTextRange == nil){
        textField.text = [textField.text substringToIndex:20];
    }
}

//iOS9以下,中文输入的情况下,点击联想字输入是不会回调

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // 如果是删除键
    if ([string length] == 0 && range.length > 0)
    {
        NSString *originString = textField.text;
        NSString *rangeString = [originString substringWithRange:range];
        NSUInteger newCount = [originString length] - [rangeString length] + [string length];
        
        _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(MIN(newCount, 20)), @(20)];return YES;
    }
    
    NSString *originString = textField.text;
    NSString *rangeString = [originString substringWithRange:range];
    NSUInteger newCount = [originString length] - [rangeString length] + [string length];
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(MIN(newCount, 20)), @(20)];
    
    NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (str.length > 20) {
        textField.text = [str substringToIndex:20];
        return NO;
    }
    
    return YES;
}

2.最后的效果图

 

posted on 2018-01-18 11:11  麦芽呀~  阅读(1273)  评论(0编辑  收藏  举报