美文网首页iOS 开发之路
iOS UITextfield输入字数限制,亲测可用

iOS UITextfield输入字数限制,亲测可用

作者: Roadsourth | 来源:发表于2018-08-06 13:34 被阅读43次

- (void)limitTextViewTextLength{

    NSString*toBeString =self.classNameTF.text;

    NSArray *currentar = [UITextInputMode activeInputModes];

    UITextInputMode*current = [currentarfirstObject];

   if([current.primaryLanguageisEqualToString:@"zh-Hans"]) 

{

        UITextRange*selectedRange = [self.classNameTFmarkedTextRange];

        UITextPosition*position = [self.classNameTFpositionFromPosition:selectedRange.startoffset:0];

        if(!position) 

        {

            if(toBeString.length>4)

       {

                self.classNameTF.text= [toBeStringsubstringToIndex:4];

            }

        }

    }

    else

{

        if(toBeString.length>4)

 {

            self.classNameTF.text= [toBeString substringToIndex:4];

        }

    }

}

相关文章

网友评论

    本文标题:iOS UITextfield输入字数限制,亲测可用

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