【问题标题】:I want to get the content(NSString) whenever TextField content change每当 TextField 内容更改时,我都想获取内容(NSString)
【发布时间】:2012-11-23 14:37:19
【问题描述】:
- ( 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 字节语言的字符时存在删除部分的问题。 你知道简单的方法吗?

感谢您的阅读。

【问题讨论】:

  • 你想达到什么目的?发布的代码对文本进行了一些有趣(损坏)的计算,但我不清楚它的含义。

标签: iphone textfield


【解决方案1】:

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

NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多