美文网首页
UItextfield limit chinese word

UItextfield limit chinese word

作者: Gaizka | 来源:发表于2019-01-11 15:11 被阅读18次

    #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

    相关文章

      网友评论

          本文标题:UItextfield limit chinese word

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