Git Product home page Git Product logo

atdemo's Introduction

ATDemo

用于仿微博、微信、钉钉的艾特(@)功能【话题功能类型处理】【swift 已经支持】

  • 艾特所在的特殊文本不支持编辑,为一个整体存在

一、实现功能

  • 支持OC和Swift
  • 以UITextView为基础实现,可以输入时支持特殊文本变色
  • 支持 特殊文本列表输出(包含在文本中的定位信息、可以自定义其它内容),减少服务器的交互
  • 富文本用 YYLabel显示,支持可点击
  • 输入时,支持监测艾特会自动唤起其它界面
  • 最大字数限制控制功能,暂不支持

二、效果图

三、如何使用

与UITextView使用一样,只是方法的拓展【Swift 同理调用】

1、导入头文件

#import "ATTextView.h"

2、使用时,集成ATTextView即可

3、插入话题或人名等特殊文本时,需要更新光标位置

    NSArray *results = self.textView.atUserList;

    NSLog(@"输出打印:");
    for (TextViewBinding *model in results) {
        NSLog(@"user info - name:%@ - location:%ld",model.name, model.range.location);
    }

4、获取已经拆入的话题或人名等特殊文本列表

    [weakSelf.textView insertWithBindingModel:bindingModel];

四、说重点!!!

1、通过实现UITextViewDelegate中的三个方法完成主要的核心操作

用于处理光标移动的逻辑

- (void)textViewDidChangeSelection:(UITextView *)textView

文本有改变时,重置attributedText属性

- (void)textViewDidChange:(UITextView *)textView

文本进行增、删、改时的处理逻辑

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

2、通过正则表达时,查找定位@符号列表数据

#define kATRegular      @"@[\\u4e00-\\u9fa5\\w\\-\\_]+ "
- (NSArray<ATTextViewBinding *> *)getResultsListArrayWithTextView:(NSAttributedString *)attributedString {
    __block NSMutableArray *resultArray = [NSMutableArray array];
    NSRegularExpression *iExpression = [NSRegularExpression regularExpressionWithPattern:kATRegular options:0 error:NULL];
    [iExpression enumerateMatchesInString:attributedString.string
                                  options:0
                                    range:NSMakeRange(0, attributedString.string.length)
                               usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        NSRange resultRange = result.range;
        NSString *atString = [attributedString.string substringWithRange:result.range];
        ATTextViewBinding *bindingModel = [attributedString attribute:ATTextBindingAttributeName atIndex:resultRange.location longestEffectiveRange:&resultRange inRange:NSMakeRange(0, atString.length)];
        if (bindingModel) {
            bindingModel.range = result.range;
            [resultArray addObject:bindingModel];
        }
    }];
    return resultArray;
}

五、文档参考

为了研究这个 艾特 功能花费了大量的时间和精力,也参考了网上许多的案例实现。

以下为主要参考文档链接,需要请查看:

1、iOS中@功能的完整实现

2、UITextView中,如何对特殊文本进行整体绑定

更多问题请issue me!!!

atdemo's People

Contributors

monkey6468 avatar

Stargazers

selice avatar xiaojiguang avatar codeman avatar wangjin avatar  avatar 勤奋的笨老头 avatar  avatar  avatar steve7688 avatar  avatar 武夫裴钱 avatar

Watchers

 avatar

atdemo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.