代码如下:

static constNSInteger Max_Character_Num =4;

    - (IBAction)onTextFieldEditingChanged:(id)sender   
    {   
        NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage];   
        if ([lang isEqualToString:@"zh-Hans"]) {   
            UITextRange *selectedRange = [self.tagNameTextField markedTextRange];   
            UITextPosition *position = [self.tagNameTextField positionFromPosition:selectedRange.start offset:0];   
            if (!position) {   
                if (self.tagNameTextField.text.length > Max_Character_Num) {   
                    self.tagNameTextField.text = [self.tagNameTextField.text substringFrom:0 to:Max_Character_Num];   
                }   
            }   
        }else{   
            if (self.tagNameTextField.text.length > Max_Character_Num) {   
                self.tagNameTextField.text = [self.tagNameTextField.text substringFrom:0 to:Max_Character_Num];   
            }   
        }   
    }  
来自:http://blog.csdn.net/wang_lichun/article/details/45251217
评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!