views:

63

answers:

3

I have a UITextView in which I want the initial value to be "@username " when they keyboard shows up (notice the space after the username). This way the user can start typing right away without having to tap space. So I do:

textView.text = [NSString stringWithFormat:@"@%@ ", username];

But it seems like it's impossible to have the UITextView text property ending with a space (it always gets stripped out). Am I correct? Is there any workaround for this?

I tried using the \s char and no success.

EDIT: see first answer below. Bug has been submitted to Apple — #8038616

A: 

NSString does not strip whitespace unless you ask it to. If there is no space in the UI, something else is stripping the spaces.

bbum
A: 

I tested this through an NSLog statement and could not replicate your symptom:

NSLog(@"@%@ ", @"test");

While the whitespace is not directly visible, it can be made visible by highlighting text in the console.

It is likely your problem is somewhere else.

Alex Reynolds
You're right. Logging my string does log the space at the end. But the UITextView doesn't display it (ie the cursor gets right after the end of the word, no space)
Sam V
+1  A: 

I can find similar questions to yours using Google (i.e. http://www.iphonedevsdk.com/forum/iphone-sdk-development/44039-uitextview-whitespace-cursor-position.html) so I'm actually inclined to think this might be a bug with UITextView, where it's trimming whitespace in its text setter method. I'd suggest filing a bug.

Edit: this only seems to happen when the UITextView is not in editing mode. When in editing mode with the keyboard raised, you can set text, like Twitterific. However, the trimming behaviour present, when the UITextView is not in editing mode, is a bug -- please report it so it can get fixed (and let us know the # if you do).

Shaggy Frog
Can you think of any workaround?
Sam V
If the setter really is trimming text behind the scenes, there's probably nothing you can do about it, since the `text` property is the only way to give a UITextView text.
Shaggy Frog
Many Twitter clients seem to be able to do this. Tweetie and Twitterrific are both handling this fine.
Sam V
Trying with Twitterific now. I only see this behaviour when a UITextView is in editing mode (keyboard raised). That's altogether different.
Shaggy Frog
In my case the keyboard is raised as well. How is this different?
Sam V
If you read the question again, you'll see the OP is trying to programatically set text to the UITextView, but when the OP tries to then immediately recover the string, the trailing whitespace is missing. Which makes me think the class has a bug/undocumented feature to perform this trimming behaviour in the setter for the `text` property.
Shaggy Frog
Sorry but that doesn't answer my question. How do Twitterrific and Tweetie do it then? If they do it, then there must be a way.
Sam V
Put your `UITextView` in editing mode. Then try setting the `text` property. *If* you want the keyboard raised.
Shaggy Frog
Awesome. This works. The setter call was in the textViewDidBeginEditing: delegate method and it didn't work (no space). I had to call the setter after a short delay, after the keyboard is up — I thought this was the moment when the delegate method was being called. But it doesn't look like it.
Sam V
Please edit the answer (textview in editing mode vs not in editing mode) and I'll accept it.
Sam V