0
- ( 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 字节语言的字符时存在删除部分的问题。你知道简单的方法吗?

感谢您的阅读。

4

1 回答 1

0

从您的问题中不清楚您要达到的目标。如果要在编辑后计算结果字符串:

NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
于 2012-11-23T14:50:47.503 回答