(void)limittextfieldlength:(uitextfield *)textfield maxlength:(nsinteger)maxlength { 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 > maxlength) { textfield.text = [tobestring substringtoindex:maxlength]; } } else { // 有高亮选择的字符串,则暂不对文字进行统计和限制 } } else { // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况 if (tobestring.length > maxlength) { textfield.text = [tobestring substringtoindex:maxlength]; } } } (void)limittextviewlength:(uitextview *)textview maxlength:(nsinteger)maxlength { nsstring *tobestring = textview.text; nsstring *lang = [[textview textinputmode] primarylanguage]; if ([lang isequaltostring:@"zh-hans"]) { uitextrange *selectedrange = [textview markedtextrange]; uitextposition *position = [textview positionfromposition:selectedrange.start offset:0]; if (!position) { if (tobestring.length > maxlength) { textview.text = [tobestring substringtoindex:maxlength]; } } } else { if (tobestring.length > maxlength) { textview.text = [tobestring substringtoindex:maxlength]; } } }
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....