0

I want to create a chat application as shown in the image below:

enter image description here

The color is a UITextView. But the problem is how can I make the text (Entered from the UITextField) display on the bottom of the UITextView ?

Usually, when I try to append text, it always displays from the top. But, How can I display from the bottom ?

Note: I have been stuck in this very problem for days, and I dont have any useful code to demonstrate.

Illep
  • 16,375
  • 46
  • 171
  • 302
  • Why UITextView and not UITableView? Also, the code that isn't working is the useful code to present here. In this case, lets see where you add the text to the text view. – danh Mar 16 '16 at 18:58

2 Answers2

0

You just need to scroll the textview content to the bottom... pretty simple actually:

-(void)scrollTextViewToBottom:(UITextView *)textView {
     if(textView.text.length > 0 ) {
        NSRange bottom = NSMakeRange(textView.text.length -1, 1);
        [textView scrollRangeToVisible:bottom];
     }
}

You would just call that like this:

// assuming you named your UITextView textView
[self scrollTextViewToBottom:self.textView];

Theres a few methods for doing this, heres a link to a question dealing with this. Just find your favorite answer and apply it to your text view:

Scroll UITextView to bottom

Community
  • 1
  • 1
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
0

You should look into using a UITableView for this use case, where each message is contained within its own UITableViewCell. Using one UITextView is bad practice for several reasons - scroll performance, user interaction (tapping on a cell), management of content, etc.

Ryan Friedman
  • 293
  • 3
  • 7
  • I tired. But, I was not able to load the items bottom up from the TableView. Any solution for this ? – Illep Mar 17 '16 at 02:58
  • The tableView will load content in the order the data source supplies it. Your use case is actually very standard, you'd append the content from the UITextField to the end of an Array, which the tableView would then pull content from. – Ryan Friedman Mar 17 '16 at 20:52