Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

How to add a line to a UITextView

vincefriedvincefried Hamburg, GermanyPosts: 48Registered Users @
edited April 2011 in iPhone SDK Development
Hi everybody!

I am currently programming on something like a peer-to-peer Chat-App. I have a textfield and a textview.
-(IBAction) sendData:(id)sender{
    
	NSString *str=mTextField.text;
	[mSession sendData:[str dataUsingEncoding: NSASCIIStringEncoding] toPeers:mPeers withDataMode:GKSendDataReliable error:nil];

}

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{    
	NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	NSLog(@"Received Data from %@",peer);
	mTextView.text=aStr;
	
}


When I type in something in, the text of the textview appears. When I repeat that the text CHANGES. But I want to add a line. Could somebody please tell me how to do that?

vincefried
Post edited by vincefried on

Replies

  • reficulreficul Posts: 190Registered Users @ @
    edited April 2011
    You can try to use stringByAppendingString.
    mTextview.text = [mTextview.text stringByAppendingString:aStr];
    
    Remember to release your alloced string.
    <font face="Century Gothic"><font size="1"><font color="Red">If you really want something in this life you have to work for it. Now quiet, they're about to announce the lottery numbers.</font><br />
    <br />
    Homer J Simpson</font></font>
  • Duncan CDuncan C Posts: 8,994Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited April 2011
    vincefried wrote: »
    Hi everybody!

    I am currently programming on something like a peer-to-peer Chat-App. I have a textfield and a textview.
    -(IBAction) sendData:(id)sender{
        
    	NSString *str=mTextField.text;
    	[mSession sendData:[str dataUsingEncoding: NSASCIIStringEncoding] toPeers:mPeers withDataMode:GKSendDataReliable error:nil];
    
    }
    
    - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
    {    
    	NSString* aStr;
    	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    	NSLog(@"Received Data from %@",peer);
    	mTextView.text=aStr;
    	
    }
    
    

    When I type in something in, the text of the textview appears. When I repeat that the text CHANGES. But I want to add a line. Could somebody please tell me how to do that?

    vincefried


    Several things:

    In one part of your code you call it mTextField, and in another part you call t mTextView. Do you have two different text objects?

    Are you using UITextView or UITextField objects? A UITextView is a multi-line, scrollable text object.

    If you are creating UITextFields, though, I don't think they are set up to display multiple lines of text.

    If you're using a UILabel (read-only) they default to a single line, but can be set up to display multiple lines. To do that set the numberOfLines property to 0.

    Next, assuming you're using a UITextView, you will need to append new text to the end of the old. To get the new text to begin on a new line, you'll need to add a carriage return at the end of the old text. Something like this:

    myTextView.text = [myTextView.text stringByAppendingFormat: @"\n%@", newText];
    
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • vincefriedvincefried Hamburg, GermanyPosts: 48Registered Users @
    edited April 2011
    ... I didn't describe it right... first you connect over peer-to-peer to another device. Then, you have a textfield and a textview. in the textfield, you type in something, and then it displays the typed text in the textview of your peer. the textview is not editable. there's a "send"-button, which is connected to the sendData-action. everytime, I press the send button, the text in the textview of my peer will be replaced by the new text, I typed in, but it should be added in a new line, just like a messenger. I hope that helped ;)
  • Duncan CDuncan C Posts: 8,994Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited April 2011
    vincefried wrote: »
    ... I didn't describe it right... first you connect over peer-to-peer to another device. Then, you have a textfield and a textview. in the textfield, you type in something, and then it displays the typed text in the textview of your peer. the textview is not editable. there's a "send"-button, which is connected to the sendData-action. everytime, I press the send button, the text in the textview of my peer will be replaced by the new text, I typed in, but it should be added in a new line, just like a messenger. I hope that helped ;)

    Ok, so take the contents of your text field and/or the response from the server and append it to the contents of the text view as I outlined in my post.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • vincefriedvincefried Hamburg, GermanyPosts: 48Registered Users @
    edited April 2011
Sign In or Register to comment.