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
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How to add a line to a UITextView

vincefriedvincefried Posts: 43Registered Users
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: 185Registered Users
    You can try to use stringByAppendingString.
    mTextview.text = [mTextview.text stringByAppendingString:aStr];

    Remember to release your alloced string.
    If you really want something in this life you have to work for it. Now quiet, they're about to announce the lottery numbers.



    Homer J Simpson
  • Duncan CDuncan C Posts: 8,021Tutorial Authors, Registered Users
    vincefried;319186 said:
    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

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • vincefriedvincefried Posts: 43Registered Users
    ... 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,021Tutorial Authors, Registered Users
    vincefried;319283 said:
    ... 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

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • vincefriedvincefried Posts: 43Registered Users
Sign In or Register to comment.