views:

1089

answers:

2

Hi there!

Does anyone know how to create a text field that has a UIImage background and does word-wrapping?

It appears that word-wrapping is not available on UITextField, while a background image is not available for UITextView! Also, it should change the size of the control or at least alert that the number of lines changed so that a delegate could change its size..

An example of such control is the input field for messages in the SMS application.

Edit: the code should also get the text field to always show the text being edited. Apparently, when UITextView changes size while editing it somehow loses focus on the current text.

Thanks ahead! Aviad.

+1  A: 

Word-wrapping is not available on UITextField because UITextField only supports single-line text.

Use a UITextView. Make textView.backgroundColor = [UIColor clearColor]. Add the UIImage first, then the UITextView on top of it. Make sure the sizes are correct, and you should be fine.

As for changing the size of the UITextView, in your UITextViewDelegate, do something like this:

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *s = textView.text;
    CGSize testSize = CGSizeMake(textView.frame.size.width, NSIntegerMax);
    CGSize neededSize = [s sizeWithFont: textView.font constrainedToSize: testSize     lineBreakMode: UILineBreakModeWordWrap]; // or UILineBreakModeCharacterWrap?

    if (neededSize.height > testSize.height)
    {
        // grow textView
        textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, neededSize.width, neededSize.height);

        // then adjust the image size -- something like this
        imageView.frame = textView.frame 
    }

}
Amagrammer
As I said, I want a behavior similar to the one in the SMS application coming with the iPhone. When the text needs wrapping, the whole component needs to grow (including the image, presumably using UIImage's stretchableImage message).
Aviad Ben Dov
Is that better?
Amagrammer
Something odd about the code. First off, there is no "textSize", but you might have meant "testSize". Second, and most weird, is the "neededSize.height > testSize.height", which would always return false as testSize's height is NSIntegerMax (init'd two lines earlier and never changed..)Am I missing something?
Aviad Ben Dov
I do get the idea though. I'll give it a go and see how it works.
Aviad Ben Dov
Yeah, sorry, I tossed that off quickly. The compiler here on Stack Overflow doesn't seem to catch most syntax errors...I may have been wrong about what sizeWithFont will return if you set the constraint to max_int. Not my best answer.
Amagrammer
sizeWithFont will be okay, I know the message well. I'm now stuck with how to move all the other views "upwards" when the event occurs. I might have to contain all of the upper area in a specific UIView and then change its frame. Any other idea?
Aviad Ben Dov
A: 

Eventually I wrote my own code, which worked to some degree with a lot of workarounds (changing the focus for example, resizing when removing all the text again, etc). If anyone's interested, I'll post the code.

In other news, just today I saw in a different question around here something called IFVerticallyExpandingTextField. The name was promising, the code is here, and I'll give it a look. Anyone with a similar problem, you might want to see this too.

My mistake, I didn't read it carefully enough: IFVerticallyExpandingTextField is for the Mac, not the iPhone.

Aviad Ben Dov