如何限制textField中输入字符的个数
2个回答
2014-12-29
展开全部
一般,当我们需要限制textfield的输入字符限制时,调用- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string方法来实现,比如:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (self.remarkname == textField) //判断是否时我们想要限定的那个输入框
{
NSString * toBeString = [self.remarkname.text stringByReplacingCharactersInRange:range withString:string];
if (range.location >= 8)
{
return NO;
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (self.remarkname == textField) //判断是否时我们想要限定的那个输入框
{
NSString * toBeString = [self.remarkname.text stringByReplacingCharactersInRange:range withString:string];
if (range.location >= 8)
{
return NO;
}
return YES;
}
展开全部
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location >= 2)
return NO; // return NO to not change text
return YES;
}
//限制长度为2
{
if (range.location >= 2)
return NO; // return NO to not change text
return YES;
}
//限制长度为2
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询