- ( 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字节字符的字符时,此代码在删除部分时出现问题。 你知道简单的方法吗?
感谢您的阅读。
答案 0 :(得分:0)
从您的问题中不清楚您要实现的目标。如果要在编辑后计算结果字符串:
NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];