#WUTextSuggestion
A text suggestion toolkit for iOS.
Download and run the demo project to see it in action.
##What can it do?
WUTextSuggestion
is still early in development, it currently supports @ (at) and # (hashtag, twitter style) suggestions for UITextView
.
WUTextSuggestion
aims to be a full featured text suggestion toolkit for iOS.
It can easily be integrated in your project with only couple lines of code.
It allows you to load text suggestions asynchronously from a remote server.
It is fully customizable. You can design your own text suggestion display controller to work with it.
##What's inside?
WUTextSuggestion
is consists of two parts.
####WUTextSuggestionController
WUTextSuggestionController
provides the text searching and checking function. It tells you when and how you should give your user text suggestions.
####WUTextSuggestionDisplayController
WUTextSuggestionDisplayController
, a text suggestion display controller based on UIMenuController
. It asks it's dataSource
for the text suggestions, and display them beautifully on the screen.
##Usage
Using WUTextSuggestionDisplayController
to display text suggestions.
-
Setup.
//Create a WUTextSuggestionDisplayController and assign the dataSource. WUTextSuggestionDisplayController *suggestionDisplayController = [[WUTextSuggestionDisplayController alloc] init]; suggestionDisplayController.dataSource = self; //Create a WUTextSuggestionController with a textView and the suggestionDisplayController you just created. WUTextSuggestionController *suggestionController = [[WUTextSuggestionController alloc] initWithTextView:self.textView suggestionDisplayController:suggestionDisplayController]; //Set the suggestion type suggestionController.suggestionType = WUTextSuggestionTypeAt | WUTextSuggestionTypeHashTag;
-
Provide the suggestions.
You need to provide the text suggestions based on
suggestionType
andsuggestionQuery
.When a user typed
@na
, thesuggestionType
will beWUTextSuggestionTypeAt
, and thesuggestionQuery
will bena
.You need to wrap the suggestions in
WUTextSuggestionItem
objects, return an array of WUTextSuggestionItem.A
WUTextSuggestionItem
has atitle
and acustomActionBlock
.title
is the suggesting text.customActionBlock
, if assigned, will be executed after user tapped that text suggestion.//WUTextSuggestionDisplayControllerDataSource - (NSArray *)textSuggestionDisplayController:(WUTextSuggestionDisplayController *)textSuggestionDisplayController suggestionDisplayItemsForSuggestionType:(WUTextSuggestionType)suggestionType query:(NSString *)suggestionQuery { //return an array of WUTextSuggestionItem. }
You can also chose to use
-textSuggestionDisplayController:suggestionDisplayItemsForSuggestionType:query:callback:
for async data loading. -
Done. There's a demo project
WUTextSuggestionDemo
.
-
You need to use
- initWithTextView:
to create an instance ofWUTextSuggestionController
. -
The
textView
will hold a strong pointer to thetextSuggestionController
which can be acquired usingtextSuggestionController
property of thetextView
. -
Set the
suggestionType
property of thetextSuggestionController
. -
Listen to the callback and present your custom text suggestion view.
//1.
WUTextSuggestionController *suggestionController = [[WUTextSuggestionController alloc] initWithTextView:self.textView];
//3.
suggestionController.suggestionType = WUTextSuggestionTypeAt | WUTextSuggestionTypeHashTag;
//4.
[suggestionController setShouldBeginSuggestingBlock:^{
//"@" or "#" detected. You should prepare your text suggestion view.
}];
[suggestionController setShouldReloadSuggestionsBlock:^(WUTextSuggestionType type, NSString *query, NSRange range) {
//User typed something after the "@" or "#", you should show your text suggestion view with suggestions.
}];
[suggestionController setShouldEndSuggestingBlock:^{
//Suggesting end. You should hide your text suggestion view.
}];
##Roadmap
- UITextField support.
##Requirements
- Automatic Reference Counting (ARC)
- iOS 5.0+
- Xcode 4.5+
##Contributing
If you find a bug and know exactly how to fix it, please open a pull request.
If you can't make the change yourself, please open an issue after making sure that one isn't already logged.
##License
The MIT license, as aways.