• 仿微信表情输入


    哇哦,好久没有写过博客了,前段时间太忙了。最近项目需求,要实现一个类似微信的的表情输入,于是把微信的表情扒拉出来,实现了一把。可以从这里下载源码。看起来表情输入没有多少东西,不外乎就是用NSTextAttachment来实现图文混排,结果在实现的过程中遇到了很多小问题,接下来会一一介绍遇到过的坑。先上一张效果图:

    效果

    一、实现表情选择View(WKExpressionView)

    具体的实现就不细说了,主要功能就是点击表情时,将对应表情的图片名称通知给delegate。

    二、实现表情textView(WKExpressionTextView)

    WKExpressionTextView继承自UITextView, 提供 
    - (void)setExpressionWithImageName:(NSString *)imageName fontSize:(CGFloat)fontSize方法,用于根据图片插入表情。 具体实现:

    //富文本
        WKExpressionTextAttachment *attachment = [[WKExpressionTextAttachment alloc] initWithData:nil ofType:nil];
        UIImage *image = [UIImage imageNamed:imageName];
        attachment.image = image;
        attachment.text = [WKExpressionTool getExpressionStringWithImageName:imageName];
        attachment.bounds = CGRectMake(0, 0, fontSize, fontSize);
        NSAttributedString *insertAttributeStr = [NSAttributedString attributedStringWithAttachment:attachment];
        NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    
        //在当前编辑位置插入字符串
        [resultAttrString insertAttributedString:insertAttributeStr atIndex:self.selectedRange.location];
    
        NSRange tempRange = self.selectedRange;
    
        self.attributedText = resultAttrString;
    
        self.selectedRange = NSMakeRange(tempRange.location + 1, 0);
    
        [self.textStorage addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:_defaultFontSize]} range:NSMakeRange(0, self.attributedText.length)];
    
        [self scrollRangeToVisible:self.selectedRange];
    
        [self textChanged];
    

     其中WKExpressionTextAttachment继承自NSTextAttachment, 并新增text字段,为了保存表情对应的文本,用于复制粘贴操作。

    @interface WKExpressionTextAttachment : NSTextAttachment
    
    @property (nonatomic, copy) NSString *text;
    
    @end
    

     WKExpressionTool的提供将普通字符串转换为富文本的方法,主要用于复制时生成表情。 
    主要方法

    + (NSAttributedString *)generateAttributeStringWithOriginalString:(NSString *)originalString fontSize:(CGFloat)fontSize
    {
        NSError *error = NULL;
        NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithString:originalString];
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\[[a-zA-Z0-9u4e00-u9fa5]{1,}\]" options:NSRegularExpressionAllowCommentsAndWhitespace error:&error];
    
    
        NSArray *results = [regex matchesInString:originalString options:NSMatchingReportCompletion range:NSMakeRange(0, originalString.length)];
        if (results) {
            for (NSTextCheckingResult *result in results.reverseObjectEnumerator) {
                NSRange resultRange = [result rangeAtIndex:0];
    
                NSString *stringResult = [originalString substringWithRange:resultRange];
    
                NSLog(@"%s %@
    ", __FUNCTION__, stringResult);
    
                NSAttributedString *expressionAttrString = [self getAttributeStringWithExpressionString:stringResult fontSize:fontSize];
    
                [resultAttrString replaceCharactersInRange:resultRange withAttributedString:expressionAttrString];
            }
    
        }
        return resultAttrString;
    }
    
    /**
     *  通过表情生成富文本
     *
     *  @param expressionString 表情名
     *  @param fontSize         对应字体大小
     *
     *  @return 富文本
     */
    + (NSAttributedString *)getAttributeStringWithExpressionString:(NSString *)expressionString fontSize:(CGFloat)fontSize
    {
    
        NSString *imageName = [self getExpressionStringWithImageName:expressionString];
    
        WKExpressionTextAttachment *attachment = [[WKExpressionTextAttachment alloc] initWithData:nil ofType:nil];
        UIImage *image = [UIImage imageNamed:imageName];
        attachment.image = image;
        attachment.text = [WKExpressionTool getExpressionStringWithImageName:imageName];
        attachment.bounds = CGRectMake(0, 0, fontSize, fontSize);
        NSAttributedString *appendAttributeStr = [NSAttributedString attributedStringWithAttachment:attachment];
    
        return appendAttributeStr;
    
    }
    

    至此,基本功能实现完成。 接下来说说遇到的小问题

    • 编辑是应该对应selectedRange
    • 复制粘贴操作需要重新实现
    • textView在插入NSTextAttachment后,会默认把font的size修改为12,需要记录默认的size

    对应selectedRange操作

    具体的操作查看源码

    重新实现copy、cut方法

    进行复制、粘贴操作会发现,不能对图片进行复制,所以需要自己重写copy、cut方法

    - (void)copy:(id)sender
    {
        NSAttributedString *selectedString = [self.attributedText attributedSubstringFromRange:self.selectedRange];
        NSString *copyString = [self parseAttributeTextToNormalString:selectedString];
    
        UIPasteboard *pboard = [UIPasteboard generalPasteboard];
        if (copyString.length != 0) {
            pboard.string = copyString;
        }
    }
    
    - (void)cut:(id)sender
    {
        [self copy:sender];
    
        NSMutableAttributedString *originalString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        [originalString deleteCharactersInRange:self.selectedRange];
        self.attributedText = originalString;
    
        NSLog(@"--%@", NSStringFromRange(self.selectedRange));
        [self textChanged];
    }
    

    记录默认font的size

    利用实例变量defaultFontSize,在WKExpressionTextView实例化时记录self.font.pointSize,以后需要取font的size时,直接取defaultFontSize

    @interface WKExpressionTextView : UITextView 
    
    @property (nonatomic, assign) CGFloat defaultFontSize;
    
    @end
    
    @implementation WKExpressionTextView
    
    {
        CGFloat _defaultFontSize;
    }
    
    - (void)awakeFromNib
    {
        [self setup];
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setup];
        }
        return self;
    }
    
    - (void)setup
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextViewTextDidChangeNotification object:self];
    
        _defaultFontSize = self.font.pointSize;
    
        self.delegate = self;
    }
    
  • 相关阅读:
    2019重新起航:搭建个人博客
    Alibaba Nacos:搭建Nacos平台
    ELK:使用docker搭建elk平台
    SpringBoot:关于默认连接池Hikari的源码剖析
    git常用命名:自用,持续更新
    linux常用命名汇总:自用,持续更新
    升级项目版本:SpringBoot1.5.x到SpringBoot2.0.x
    Python学习第十五课——类的基本思想(实例化对象,类对象)
    Python学习第十三课——re(正则表达式)模块
    Python学习第十四课——面向对象基本思想part1
  • 原文地址:https://www.cnblogs.com/pretty-guy/p/5427224.html
Copyright © 2020-2023  润新知