Implementing Autocompletion in iPhone UITextField for contacts in address book - iphone

I would like to have a UITextField or UITextView in where as I do some input, alternatives will appear something similar to when you type an address in Mail Application, alternatives appear down and is possible tap them so get a better input user interface.(since there is no need to type the complete word or address or phone number)
I do know how to fetch data from Address Book framework, also how to input text in UITextField/UITextView and its delegates but I don't know what kind of structure to use for fetching and showing data as the user do his/her input.
I know basic CoreData if this matters,
I hope I can get some help.
UPDATE (2010/3/10):
I don't have problem make a native-like GUI but I am asking about the algorithm, does any body knows what kind algorithm is best for this thing? maybe some binary tree?
Or should I just fetch data from coredata everytime?
Thanks
Ignacio
UPDATE (2010/03/28):
I've been very busy these days, so I have not tried UISearchResults but it seems fine to me. BUT I wonder was there a necessity of deletion of the wining answer? I don't think is fair my reputation went down and couldn't see the winning answer. ;(

You don't need some advanced algorithm to do this kind of thing... if you want to search the address book, then you can do so each time the user types in a character (or however frequent you need to seach). To do this, just take a look at the UISearchDisplayController class. I learned how to do almost the exact thing by looking at Apple's TableSearch sample app.
That app searches a list of objects using different fields (All, Device, Desktop, Portable)... so you could adapt it to Address Book fields (First Name, Last Name, Address...). The only thing you need to change is the search within the Address Book. I don't know exactly what your requirements ask for but this should be what you need to get it done. If you have any trouble with the code let me know... but this example really helped me, so hopefully it works for you.

I was looking for the same thing a while ago. Something that people kept suggesting was the Three20 project (google it).
For my needs this was overkill because it requires the whole project to build and I didn't want the whole project. Plus it's more fun to try it yourself :)
I ended up starting from scratch and making my own:
I started out with a subclass of a UIScrollView to contain the different controls. I subclassed a UITextField and overrided "editingRectForBounds" to support multiple lines. The bit where the contacts are displayed is just a UITableView with a background color of:
[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
And separator color:
[UIColor colorWithWhite:0.85 alpha:1];
This and the use of a shadow makes it looks like it's sunken slightly under the UITextField. I create the shadow with a custom UIView, loading it once and hiding it when required, but it works just as well with an image.
Finally, I made the blue pill shapes with a custom UIView which can intercept "touchesBegan" to know when they should change color.
Adding them is a simple matter of calculating where they need to go and using:
[myTextField addSubview:myBlueView];
Hope that helps!

Related

Is UITextInput missing selection handling mechanics?

If you implement UITextInput on your custom view and - say - use CoreText to render the text you get to a point where you can draw your own cursor and selection/marking and have that fully working with the hardware keyboard. If you switch to Japanese input then you see the marking, but there's something curious: if you long press into the marking you get the rectangular system loupe and selection handling without having to deal with the touches yourself.
What I don't get why we would have to implement our own touch handling for the selection, draw our own loupes etc. It's working for marking! So what do I have to do to get the standard gesture recognizers added to my custom view as well?
the one sample on the dev site only has a comment about that user selection would be outside the scope of the sample. Which would indicate that indeed you have to do it yourself.
I don't think that it is in Apple's interest that all developers doing their own Rich Text editor class keep doing their own selection handling code, let alone custom drawing of the round and rectangular loupes?! Granted you can try to reverse engineer it such that it comes really close, but that might give users a strange feeling if the selection mechanics differ ever so slightly.
I found that developers are split in two groups:
1) rapes UIWebView with extensive JavaScript code to make it into an editor
2) painstakingly implements the selection mechanics and loupe drawing themselves
So what is the solution here? Keep submitting Radars until Apple adds this missing piece? Or is this actually already existing (as claimed by the aforementioned engineer I met) and we are just too stupid to find how to make use of it, instead resorting to doing everything (but marked text) manually?
Even the smart guys at Omnifocus seem to think that the manual approach is the only one working. This makes me sad, you get such a great protocol, but if you implement it you find it severely crippled. Maybe even intentionally?
Unfortunately the answer to my question is: YES. If you want to get selection mechanics on a cusrom view you have to program it yourself.
As of iOS 6 you can subclass UITextView and draw the text yourself. According to an Apple engineer this should provide the system selection for you.

How does one interact with a string drawn using UIStringDrawing/CoreText

I am attempting to work on an app that highlights certain key words. For this reason, I am looking at using either CoreText or UIStringDrawing to highlight these words (As in bold/underline/italicize/color) since UITextView doesn't seem to have these facilities (And I haven't had any luck with CATextLayer).
In either case I am able to successfully manipulate and draw the string. (I.E- I can find my important words in a string and apply the appropriate traits).
The problem I am having is that when text is drawn this way, it cannot be interacted with directly. Seeing as the user will be able to delete/add text to the text view in question, this is an important thing to have.
At the moment I have stuck my drawing routines into the drawRect: method of a UITextView subclass (Mostly just to see that I can do what I want). Does UITextView have a different method that can be overridden so that it uses my code to render its string? If not, how might I go about doing what I've described?
For an example of what I am talking about, look at how the Notes app finds and highlights phone numbers and addresses. I'm not looking for phone numbers or addresses, but it gets the point across.
After a bit of additional reading, it seems that you need to subclass/override a LOT of methods in order to create your own text view whose text you can draw. Apple's docs touch on it, but they don't go much into detail.

Bubble Chat + Emoticon + UITableViewCell

This is a question for iPhone development and I'm hopin someone can point me to the right direction on how i should go about implementing this.
I am trying to write a chat application that supports emoticons/smileys. Where the smiley/emoticon images are stored can be figured out later. I think few iphone applications out there are already doing this (i.e. skype + ebuddy(?)) but not sure what method they went for.
After searching around, there seems to be a few ways of doing this (i think):
bubble chat style which has been discussed before. UITableViewController with custom UITableViewCell. For emoticons, might have to do a whole bunch of calculations to determine where to stick a UIImageView for each emoticon.
Use UIWebView as the whole "window". Style it to look like bubble chat. Takes away any manual calculations on image smiley placements.
I have no idea what the performance is like for each of these two methods, how complex it can get etc, so any comments and guidance will help for sure. Cheers
If you use custom UITableViewCell, then I'd probably implement drawRect: instead of adding labels and images. One will probably take as long to implement as the other, but it will perform much better.
The UIWebView might be worth a short, although you will have to make sure that everything looks right there, too. Instead of using one big web page, I suggest simply throwing in a web view into each table view cell.
Personally I prefer the first approach, measuring and layout of text is not too complicated, but then I've never been the ultimate HTML guru.
I agree with #Eiko on making custom UITableViewCells, especially using drawRect instead of adding labels, images, etc.
If you used a UIWebView how would you handle updating it? A complete reload each time new text is sent? That seems like it will be a cause of issues. Once you get a long conversation reloading the entire UIWebView's contents will cause some flickering which isn't acceptable in my opinion. Also using a UIWebView would require you to have 2 complete copies of each conversation in memory. 1 as your backend data and 1 as the HTML. Where using a UITableView you have your backend data, and only enough of that will be duplicated that can fill 1 screen at a time.

How would you design a question/answer view (iPhone SDK)

I'm new to iPhone development, and I have a question on how to create a view for my application.
The view should display a problem (using formatted/syntax highlighted text), and multiple possible answers. The user should be able to click on an answer to validate it.
Currently, I am trying to use a UITableView embedding UIWebView as contentView. That allows me to display formatted text easily.
The problem is that it is a real pain to compute and adjust the height of the cells. I have to preload the webview, call sizeToFit, get its height, and update the cell accordingly. This process should be done for the problem and the answers (as they are HTML formatted text too).
It's such a pain that I am planning to switch to something else. I thought using only a big UIWebView and design everything in HTML. But I looked at some articles describing how to communicate between the HTML page and the ObjectiveC code. This seems to involve some awful tricks too...
So... that's it, I don't really know what I should do.
I guess some of you dealt with such things before, and would provide some greatly appreciated tips :)
The catch here is that the iPhone API does not yet support NSAttributedString so you can't just set the text to appear as you would like in a textview.
I saw one work around which essentially used individual UILabels to represent each attribute run. (Can't find the link now.) They used NSString UIKit extensions to calculate the position of the strings on the view and then used that to position the labels.
Another work around would be to draw the strings with their attributes to a UIImage and then just display the image. That would be the easiest solution I think.
In either case your going to have to basically recreate the data structure of an attributed string.
NSAttributedString does a lot of work for us. We really miss it when it is gone.

How do i change color of letters of sectionIndexTitlesForTableView?

Hope you all are fine and also in best of your moods.
I have a little problem kindly help me to get its solution.
my Problem is:
I am using vertical search in my application, using method sectionIndexTitlesForTableView() of tableview i get all Character listed from top to bottom at rightside.
But this letters have fixed color. i need to change this color. Since i newbie i don't know how to deal with it.
Please help to get solution.
Thanks, and sorry if you found me with wrong english.
There is no supported way to do this. Even if you had access to the index view (which you don't easily), it would not be possible because there is no NSAttributedString on iPhone, so you couldn't return color information.
The best way to achieve this is to turn off the tableview's index and generate your own from scratch, floating it over top of the tableview. But I would not recommend this approach for a new iPhone developer. It is best to spend some serious time learning to build UIs the way Apple intends you to. Once you understand Apple's UI and how it's implemented, then you can make informed decisions about whether you should break the UI rules.