为iOS开发的一个文本提示工具包。
下载并运行示例项目,看看它的实际应用。
WUTextSuggestion
目前处于早期开发阶段,目前支持为UITextView
提供诸如@ (at)
和# (hashtag, twitter style)
等建议。
WUTextSuggestion
旨在成为一个功能齐全的iOS文本提示工具包。
您只需几行代码即可轻松将其集成到项目中。
它可以异步从远程服务器加载数据。
它完全可自定义。您可以设计自己的文本建议显示控制器与其配合使用。
WUTextSuggestion
由两部分组成。
WUTextSuggestionController
提供了文本搜索和检查功能。它告诉您何时何地向用户提供文本建议。
WUTextSuggestionDisplayController
是一个基于UIMenuController
的文本建议显示控制器。它会向其dataSource
询问文本建议,并将它们美观地显示在屏幕上。
使用WUTextSuggestionDisplayController
来显示文本建议。
设置。
//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;
提供建议。
您需要根据suggestionType
和suggestionQuery
提供文本建议。
当用户键入@na
时,suggestionType
将是WUTextSuggestionTypeAt
,而suggestionQuery
将是na
。
您需要将建议包裹在WUTextSuggestionItem
对象中,返回一个WUTextSuggestionItem数组。
WUTextSuggestionItem
具有一个title
和一个customActionBlock
。
title
是建议文本。
customActionBlock
,如果指定,将在用户点击该文本建议后执行。
//WUTextSuggestionDisplayControllerDataSource
- (NSArray *)textSuggestionDisplayController:(WUTextSuggestionDisplayController *)textSuggestionDisplayController suggestionDisplayItemsForSuggestionType:(WUTextSuggestionType)suggestionType query:(NSString *)suggestionQuery
{
//return an array of WUTextSuggestionItem.
}
您还可以选择使用-textSuggestionDisplayController:suggestionDisplayItemsForSuggestionType:query:callback:
进行异步数据加载。
完成。有一个名为WUTextSuggestionDemo
的示例项目。
您需要使用 - initWithTextView:
来创建 WUTextSuggestionController
的实例。
textView
将保持对 textSuggestionController
的强指针,您可以通过访问 textView
的 textSuggestionController
属性来获取。
设置 textSuggestionController
的 suggestionType
属性。
监听回调并呈现您的自定义文本建议视图。
//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.
}];
如果您发现了一个错误并且知道如何解决,请提交一个pull request。
如果您无法自己进行更改,请在确认尚未记录后打开一个issue。
MIT许可,一如既往。