UItextfield limit chinese word

#import"ViewController.h"

#import"NSString+category.h"

@interfaceViewController ()

@property(strong,nonatomic)UITextField* textfield;

@end

#define kMaxNumber15

@implementationViewController

- (UITextField*)textfield{

if(!_textfield)

{

_textfield= [[UITextFieldalloc]initWithFrame:CGRectMake(10,100,200,40)];

_textfield.delegate=self;

_textfield.placeholder=@"请输入";

_textfield.returnKeyType=UIReturnKeyDone;

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textFiledDidChange:)

name:@"UITextFieldTextDidChangeNotification"

object:_textfield];

}

return_textfield;

}

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor= [UIColorgrayColor];

[self.viewaddSubview:self.textfield];

}

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

{

NSLog(@"打印信息:%lu-----%lu ",(unsignedlong)range.location,(unsignedlong)range.length);

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

if(toBeString.length > kMaxNumber && range.length!=1){

textField.text = [toBeString substringToIndex:kMaxNumber];

returnNO;

}

returnYES;

}

- (void)textFiledDidChange:(NSNotification*)sender

{

UITextField *textField = (UITextField *)sender.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) {

if(toBeString.length > kMaxNumber) {

textField.text = [toBeString substringToIndex:kMaxNumber];

}

}

//有高亮选择的字符串,则暂不对文字进行统计和限制

else{

}

}

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

else{

if(toBeString.length > kMaxNumber) {

textField.text = [toBeString substringToIndex:kMaxNumber];

}

}

}

//- (int)convertToInt:(NSString*)strtemp

//{

//int strlength = 0;

//char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];

//for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {

//if (*p) {

//p++;

//strlength++;

//}

//else {

//p++;

//}

//

//}

//return strlength;

//}

#pragma mark - UITextFieldDelegate

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

[textFieldresignFirstResponder];

returnYES;

}

-(void)dealloc{

[[NSNotificationCenter defaultCenter]removeObserver:self

name:@"UITextFieldTextDidChangeNotification"

object:self.textfield];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
整顿职场的柯基很威猛:这种不可怕,最可怕的是夹在一帮名校里的二本选手,人家才是最稳的。
点赞 评论 收藏
分享
11-15 17:19
湖南大学 Java
成果成果成果果:这是哪个公司的hr,这么离谱吗,我没见过用性别卡技术岗的,身边女性同学拿大厂offer的比比皆是
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务