如何删除UITextView中的单个字符 [英] How to delete single characters in an UITextView

查看:325
本文介绍了如何删除UITextView中的单个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextView,例如长度为380个字符:

  NSLog(@aTextView.text lenght%i ,aTextView.text.length); 

我现在想浏览这个文本(向后,char by char)在最后一个空格之前(例如,如果最后的单词是...这是一个例子,它想将字符串减少为...这是一个:

  for(int i = aTextView.text.length-1; i> 0; i--){

NSString * checkedChar = [NSString stringWithFormat: @%c,[aTextView.text characterAtIndex:i]];
NSLog(@I currently check:%@,checkedChar);

if([checkedChar isEqualToString: ]){
// job done
i = 0; //结束循环
} else {

我需要像[aTextView.text removeCharacterAtIndex:i]; ;

 } 

}

?我在文档中找不到任何方法,将非常感谢建议。






编辑:

  NSString * myString = aTextView.text; 

NSRange range = [myString rangeOfString:@options:NSBackwardsSearch];
NSString * oldText = [myString subStringToIndex:range.location];
NSString * newText = [myString subStringFromIndex:range.location];

NSLog(@++++ OLD TEXT ++++:%@,oldText);
NSLog(@++++ NEW TEXT ++++:%@,newText);

aTextView.text = oldText;

这会导致我的应用程序崩溃...我从 - )textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText



我得到错误信息: em>

由于未捕获异常NSInvalidArgumentException终止应用程序,原因:' - [NSCFString subStringToIndex:]:无法识别的选择器发送到实例0x4e33850 xCode给我警告subStringToIndex可能不响应...

解决方案

你不需要一个循环,请参阅NSString的 rangeOfString:options: substringToIndex:方法。例如:

  NSRange range = [myString rangeOfString:@options:NSBackwardsSearch]; 

NSString * newString = [myString substringToIndex:range.location];

希望有帮助。



不要忘记检查你的字符串是否包含空格;)


I have an UITextView which is for instance 380 characters in length:

NSLog(@"aTextView.text lenght %i", aTextView.text.length);

I now want to go through this text (backwards, char by char) and delete all characters which come before the last space (e.g. if the last words were "...this is an example", it want to reduce the string to "...this is an ":

         for (int i = aTextView.text.length-1; i > 0; i--) {

         NSString *checkedChar = [NSString stringWithFormat:@"%c", [aTextView.text characterAtIndex:i]];
         NSLog(@"I currently check: %@", checkedChar);

         if ([checkedChar isEqualToString:@" "]) {
             // job done
             i = 0; // this ends the loop
         } else {

I need something like [aTextView.text removeCharacterAtIndex:i];

         }

     }

How do I achieve this? I couldn't find any methods in the docs and would be very grateful for suggestions.


EDIT:

 NSString *myString = aTextView.text;

         NSRange range = [myString rangeOfString:@" " options:NSBackwardsSearch];
         NSString *oldText = [myString subStringToIndex:range.location];
         NSString *newText = [myString subStringFromIndex:range.location];

         NSLog(@"++++ OLD TEXT ++++: %@", oldText);
         NSLog(@"++++ NEW TEXT ++++: %@", newText);

         aTextView.text = oldText;

This crashes my app... I am calling this from - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText

I get the error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString subStringToIndex:]: unrecognized selector sent to instance 0x4e33850'

And xCode gives me the warning the subStringToIndex may not respond...

解决方案

You don't need a loop to do this - take a look at NSString' rangeOfString:options: and substringToIndex: methods. For example :

NSRange range = [myString rangeOfString:@" " options:NSBackwardsSearch];

NSString *newString = [myString substringToIndex:range.location];

Hope that helps.

NB Don't forget to check that your string definitely contains a space ;)

这篇关于如何删除UITextView中的单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆