views:

490

answers:

3

I would like to change the standard iPhone UITextField so that it looks exactly like the search text field within the UISearchBar. You can see an example of this for the SMS text entry field within the iPhone Messages application.

I do not believe that the UITextField has a style property that will meet this requirement. Any ideas?

A: 

What about UITextBorderStyleRoundedRect?

textField.borderStyle = UITextBorderStyleRoundedRect;

You can also just use a UISearchBar and handle the text manually yourself.

Brad Larson
UITextBorderStyleRoundedRect doesn't appear with rounded corners like the SearchBar TextField.
Luke
In that case, you can still use a UISearchBar, which behaves very much like a UITextField, and handle the text input in whatever way you wish.
Brad Larson
A: 

It seems that this is not supported within iPhone OS 3.0. I just used a transparent UITextField with UIImageView as the background. I believe this is how the official iPhone SMS text entry works.

Luke
A: 

If you want, you can actually extract the background image from a uisearch bar and make it a stretchable image to use as the background of a UITextField. I did the same thing and it works like a charm for me. Here's the code i used to extract it:

UIImage *img = [[[self.searchBar subviews] objectAtIndex:1] background];
NSData *imgdata = UIImagePNGRepresentation(img);

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [searchPaths objectAtIndex: 0];

[imgdata writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:@"searchbarbg.png"] atomically:YES];

the second subview of the uisearchbar is the actual text field; this is subject to change in any future os releases though.

this will save the PNG to the app's documents directory, where you can grab it and drop it into your project and use it as the text field's background. the left and top cap widths for the stretchable image are 15 pixels.

Kevlar