- ( BOOL ) textField: ( UITextField * )textField shouldChangeCharactersInRange: ( NSRange )range replacementString: ( NSString * )string {
NSString *strCurrent;
if ( range.length > 0 ) { // deleting
int iNumberOfDeletedCharacter = [ string length ];
int iNumberOfRestCharacter = [ [ textField text ] length ] - iNumberOfDeletedCharacter;
strCurrent = [ [ textField text ] substringToIndex: iNumberOfRestCharacter - 1 ];
}
else { // adding
strCurrent = [ [ textField text ] stringByAppendingString: string ];
}
MyNSLog( @"%@", strCurrent );
}
此代码在输入 2 字节语言的字符时存在删除部分的问题。你知道简单的方法吗?
感谢您的阅读。