textfield字数限制问题

2017-09-14  本文已影响62人  Moker_C

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, width, height)];

textField.returnKeyType = UIReturnKeyDone;  //return按钮为完成

textField.placeholder = @"限16个中文字";

//设置textfield的placeholder的字体大小和颜色

[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

[textField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];

//设置输入的文字的大小和颜色

[textField setFont:[UIFont systemFontOfSize:12]];

[textField setTextColor:[UIColor colorWithHexString:@"333333"]];

textField.textAlignment = NSTextAlignmentRight;

textField.delegate = self;

//监听textfield输入文字的个数

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:)

name:@"UITextFieldTextDidChangeNotification" object:self.clubNameTextField];

[self.view addSubview:textField];

#pragma mark textfield delegate

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

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

return NO;  //不允许输入空格

}

return YES;

}

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

[self.clubNameTextField endEditing:YES];

return YES;

}

- (void)textFieldDidChange:(NSNotification *)obj {

UITextField *textField = (UITextField *)obj.object;

NSString *toBeString = textField.text;

NSString *lang = [textField.textInputMode primaryLanguage];

if ([lang isEqualToString:@"zh-Hans"]) {// 简体中文输入

//获取高亮部分

UITextRange *selectedRange = [textField markedTextRange];

UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

// 没有高亮选择的字,则对已输入的文字进行字数统计和限制

if (!position || !selectedRange) {

if (toBeString.length > 16) {

NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:16];

if (rangeIndex.length == 1) {

textField.text = [toBeString substringToIndex:16];

}else {

NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 16)];

textField.text = [toBeString substringWithRange:rangeRange];

}

}

}

}else {// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况

if (toBeString.length > 16) {

NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:16];

if (rangeIndex.length == 1) {

textField.text = [toBeString substringToIndex:16];

}else {

NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 16)];

textField.text = [toBeString substringWithRange:rangeRange];

}

}

}

}

//如果需要配合键盘移动,不需要可忽略

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

//监听textfield输入文字的个数

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:)

name:@"UITextFieldTextDidChangeNotification" object:self.clubNameTextField];

}

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

#pragma mark 键盘的通知

- (void)keyboardWillShow:(NSNotification *)noti {

NSValue *frameValue = [dict valueForKey:UIKeyboardFrameEndUserInfoKey];

CGRect frame = [frameValue CGRectValue];

//获取弹键盘前的偏移量

self.offsetY = self.tableView.contentOffset.y;

CGFloat height = frame.size.height;

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, height, 0);

//将tableview滚动到textfield对应的行

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:7 inSection:0]  atScrollPosition:UITableViewScrollPositionBottom animated:NO];//这里一定要设置为NO,动画可能会影响到scrollerView,导致增加数据源之后,tableView到处乱跳

}

- (void)keyboardWillHide:(NSNotification *)noti {

self.tableView.contentInset = UIEdgeInsetsZero;

}

上一篇下一篇

猜你喜欢

热点阅读