How to replace text in UITextView with selected range?

I want to replace the text in the text of a UITextView in the selected range. This is my question. Here I mention what I did? and what do I want to do ?. I have a UITextView and the text entered below in a textview.

Ask question here, I saved a range of text in text form. The range of text is {17, 0}. I take NSRange in the delegate -(void) textViewDidChange:(UITextView *)textView.

Now I want to replace the text questionwith answer, and I want to replace the text herewith them. The UITextView.text look like this,Ask answer them "after replacing the texts.

How to replace texts with selected ranges? could you help me? Thanks in advance.

+5
source share
3 answers

... , , - , :

UITextView NSString:

NSString *textViewContent = textView.text;

:

NSString *newContent = [textViewContent stringByReplacingCharactersInRange:range withString:replacement];

:

textView.text = newContent;

, , :

textView.text = @"Ask answer them";
+4

iOS 7 textStorage textView, NSMutableAttributedString, :

[self.textView.textStorage replaceCharactersInRange:withString:];

[self.textView.textStorage replaceCharactersInRange:withAttributedString:];
+9

Good decision from the top of the head.

NSArray *Dividedstring = [[self.TextView.text] componentsSeparatedByString:@" question here "]; // this will divide the string into two parts ..one before question here and second after question here.
NSString  * firstpart = [Dividedstring objectAtIndex:0]; 
NSString  * secondpart = [Dividedstring objectAtIndex:0];

self.TextView.text = [NSString stringwithFormat:@"%@ answer here %@",firstpart,secondpart];
0
source

All Articles