NSString UITextView without replacing previous text

11,488

Solution 1

Here are some ways to overcome obstacles in iOS development:

  1. Look at the documentation for the particular class you're trying to manipulate. In this case, UITextView documentation can be found within Xcode or online.

  2. Command-Click on UITextView or any other object anywhere in your code, and it will bring you to the header file for that class. The header file will list every public method and property.

  3. Look at your existing code. I'm assuming that since you have a button that adds text to a UITextView, you understand how to set its text. 99% of the time you'll find that any setter (mutator) methods will have a corresponding getter (accessor) method. In this case, UITextView has a method called setText: and a matching method just called text.

Finally, NSString has a convenience method called stringWithFormat: that you can use to concatenate (join) two strings, among other very useful things. %@ is the format specifier for a string. For example, to combine two strings, stringOne and stringTwo, you could do the following:

NSString *string = [NSString stringWithFormat:@"%@ %@", stringOne, stringTwo];

I will leave you to come up with the answer as to how to combine NSString stringWithFormat: and UITextField text and setText: to achieve what you'd like to accomplish.


Edit: The OP was unable to figure out how to utilize the information above so a complete code sample has been provided below.

Assume you have synthesized property (possibly an IBOutlet) UITextView that you have initialized called myTextView. Assume also that we are currently in the method scope of the method that gets called (your IBAction, if you're using IB) when you tap your UIButton.

[myTextView setText:[NSString stringWithFormat:@"%@ %@", myTextView.text, @"this is some new text"]];

Explanation: myTextView.text grabs the existing text inside of the UITextView and then you simply append whatever string you want to it. So if the text view is originally populated with the text "Hello world" and you clicked the button three times, you would end up with the following progression:

Initial String: @"Hello world"
Tap one: @"Hello world this is some new text"
Tap Two: @"Hello world this is some new text this is some new text"
Tap Three: @"Hello world this is some new text this is some new text text this is some new text"

Solution 2

If all you are doing is appending text, you might find this a little simpler:

myTextView.text = [myTextView stringByAppendingString:@"suffix\n"];

I found this on UITextView insert text in the textview text. Sadly, I have not found a way to append text directly without a wholesale replacement of the text in the UITextView. It bugs me that the effort involved is proportional to the total length of the existing string and the suffix, rather than just the suffix.

Solution 3

A more efficient way to append text is to use replace() at the end:

extension UITextInput {
  func append(_ string : String) {
    let endOfDocument = self.endOfDocument
    if let atEnd = self.textRange(from: endOfDocument, to: endOfDocument) {
      self.replace(atEnd, withText: string)
    }
  }
}

Solution 4

@Jack Lawrence: Your answer doesn't cover the question completely.

The example below will not scroll neatly while running off the bottom when called every second:

self.consoleView.text = [NSString stringWithFormat:@"%@%@%@", self.consoleView.text, data, @"\n"];
[self.consoleView scrollRangeToVisible:NSMakeRange(self.consoleView.text.length, 0)];

This is caused by setText replacing the original text every time thereby resetting associated contentOffsets etc.

This was possible prior to iOS 7, but since iOS 7 it seems that setText cannot be prevented from exhibiting jumpy behaviour. Appending does not seem to be an option for TextViews in this scenario?

Share:
11,488
nfoggia
Author by

nfoggia

Hi everybody. I am a newbie programmer who is constantly trying to learn. Currently am attending college to one day become a great programmer. I am currently studying C++ and Java, and my hobby language is Objective-c for iOS development.

Updated on June 04, 2022

Comments

  • nfoggia
    nfoggia almost 2 years

    How would I go about adding text to a UITextView without replacing the previous text?

    So far I have a UITextView and a UIButton that adds the text to the UITextView, but I would like the text field to append more text every time you hit the button instead of completely deleting the text and replacing it.

  • nfoggia
    nfoggia over 12 years
    but i want it to be able to hold an unlimited amount of strings if it needs too
  • Jack Lawrence
    Jack Lawrence over 12 years
    It can, using only the methods I've outlined above.
  • Jack Lawrence
    Jack Lawrence over 12 years
    Everything you need is right there, you just need to try instead of using stack overflow as a crutch to rely on. I'll edit the question and make it a little easier for you, if I must.
  • damon
    damon over 12 years
    @nfoggia, if you already knew all of that, you wouldn't be here asking such a simple question. I have nothing against simple questions, but your tone and lack of respect for Jack's considerate answer make me wonder if you should be here at all.
  • Jack Lawrence
    Jack Lawrence over 12 years
    @damon thanks :) I've updated the question anyway so he can be lazy. ;)
  • nfoggia
    nfoggia over 12 years
    sorry about exploding earlier... i've had a bad day. haha. Thank you a ton, i actually just figured it out and i was coming to apologize and you put that up. Thanks guys.
  • HAS
    HAS over 10 years
    I think you forgot a .text: [myTextView setText:[myTextView stringByAppendingString:@"suffix\n"]]; should be [myTextView setText:[myTextView.text stringByAppendingString:@"suffix\n"]]; otherwise you are calling an NSString method on a UITextView ;) ... btw myTextView.text = [myTextView.text stringByAppendingString:@"suffix\n"]; is even shorter ;)
  • Randall Cook
    Randall Cook over 10 years
    Thanks, @HAS. I trimmed away too much when I copied from my project.
  • Reefwing
    Reefwing over 8 years
    Your answer still needs to be corrected to myTextView.text = [myTextView.text stringByAppendingString:@"suffix\n"]; as per @HAS's comment