美文网首页
iOS textfield限制输入的最大长度

iOS textfield限制输入的最大长度

作者: ljzza | 来源:发表于2017-12-04 11:00 被阅读0次
[self.textFiled addTarget:selfaction:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
-(void)textFieldDidChange:(UITextField*)textField
{CGFloatmaxLength =5;
NSString*toBeString = textField.text;
//获取高亮部分
UITextRange*selectedRange = [textField markedTextRange];
UITextPosition*position = [textField positionFromPosition:selectedRange.start offset:0];
if(!position || !selectedRange)
{
if(toBeString.length > maxLength)
{NSRangerangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];if(rangeIndex.length ==1)
{
textField.text = [toBeString substringToIndex:maxLength];
}else{NSRangerangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
textField.text = [toBeString substringWithRange:rangeRange];
}}}}

相关文章

网友评论

      本文标题:iOS textfield限制输入的最大长度

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