TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by YuAo.
A text suggestion toolkit for iOS.
Download and run the demo project to see it in action.
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.
WUTextSuggestion
is consists of two parts.
WUTextSuggestionController
provides the text searching and checking function. It tells you when and how you should give your user text suggestions.
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.
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
and suggestionQuery
.
When a user typed @na
, the suggestionType
will be WUTextSuggestionTypeAt
, and the suggestionQuery
will be na
.
You need to wrap the suggestions in WUTextSuggestionItem
objects, return an array of WUTextSuggestionItem.
A WUTextSuggestionItem
has a title
and a customActionBlock
.
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 of WUTextSuggestionController
.
The textView
will hold a strong pointer to the textSuggestionController
which can be acquired using textSuggestionController
property of the textView
.
Set the suggestionType
property of the textSuggestionController
.
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.
}];
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.
The MIT license, as aways.