IOS 开发之 UITextField限制字数的方法
IOS开发之UITextField限制字数的方法
在输入东西的时候,如果想限制最大字数,可以用下面方法:
实例代码:
-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string;
{
if([stringisEqualToString:@"\n"])
{
returnYES;
}
NSString*toBeString=[textField.textstringByReplacingCharactersInRange:rangewithString:string];
if(self.myTextField==textField)
{
if([toBeStringlength]>20){
textField.text=[toBeStringsubstringToIndex:20];
UIAlertView*alert=[[[UIAlertViewalloc]initWithTitle:nilmessage:@"超过最大字数不能输入了"delegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil,nil]autorelease];
[alertshow];
returnNO;
}
}
returnYES;
}
以上就是IOS中UITextField限制字数的方法的实现,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短