WUTextSuggestion 0.2.0

WUTextSuggestion 0.2.0

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
发布最新发布2014年12月

YuAo维护。



  • 作者:
  • Yu Ao

为iOS开发的一个文本提示工具包。

ScreenShot

下载并运行示例项目,看看它的实际应用。

它能做什么?

WUTextSuggestion目前处于早期开发阶段,目前支持为UITextView提供诸如@ (at)# (hashtag, twitter style)等建议。


WUTextSuggestion旨在成为一个功能齐全的iOS文本提示工具包。

您只需几行代码即可轻松将其集成到项目中。

它可以异步从远程服务器加载数据。

它完全可自定义。您可以设计自己的文本建议显示控制器与其配合使用。

它包含哪些内容?

WUTextSuggestion由两部分组成。

WUTextSuggestionController

WUTextSuggestionController提供了文本搜索和检查功能。它告诉您何时何地向用户提供文本建议。

WUTextSuggestionDisplayController

WUTextSuggestionDisplayController是一个基于UIMenuController的文本建议显示控制器。它会向其dataSource询问文本建议,并将它们美观地显示在屏幕上。

用法

A. 简单用法,使用WUTextSuggestionDisplayController。

使用WUTextSuggestionDisplayController来显示文本建议。

  1. 设置。

    //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;
    
    
  2. 提供建议。

    您需要根据suggestionTypesuggestionQuery提供文本建议。

    当用户键入@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:进行异步数据加载。

  3. 完成。有一个名为WUTextSuggestionDemo的示例项目

B. 与您自定义的文本建议显示控制器一起工作。

  1. 您需要使用 - initWithTextView: 来创建 WUTextSuggestionController 的实例。

  2. textView 将保持对 textSuggestionController 的强指针,您可以通过访问 textViewtextSuggestionController 属性来获取。

  3. 设置 textSuggestionControllersuggestionType 属性。

  4. 监听回调并呈现您的自定义文本建议视图。

//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.
}];

路线图

  • UITextField 支持。

要求

  • 自动引用计数(ARC)
  • iOS 5.0+
  • Xcode 4.5+

贡献

如果您发现了一个错误并且知道如何解决,请提交一个pull request。

如果您无法自己进行更改,请在确认尚未记录后打开一个issue。

许可

MIT许可,一如既往。