对含有表情字符的字符串类别的封装

对含有表情字符的字符串类别的封装

我这边是对字符串写了个类别,我主要的思路是先判断字符串是不是含有表情符号,我的表情符号的定义是这样的[得意]


得意@2x.png

然后图片名称也是一样的名字,在遍历字符串时可以将含有表情字符的找到装到一个数组中代码如下

      -(void)getMessageRange:(NSString*)message :(NSMutableArray*)array {
    
    NSRange rangeL = [message rangeOfString:@"["];
    NSRange rangeR = [message rangeOfString:@"]"];
    //判断当前字符串是否还有表情的标志。
    if (rangeL.length && rangeR.length) {
        if (rangeL.location > 0) {
            
        [array addObject:[message substringToIndex:rangeL.location]];
        [array addObject:[message substringWithRange:NSMakeRange(rangeL.location, rangeR.location + 1 - rangeL.location)]];
        
        NSString *str = [message substringFromIndex:rangeR.location + 1];
        [self getMessageRange:str :array];
    }
    else {
        NSString *nextstr = [message substringWithRange:NSMakeRange(rangeL.location, rangeR.location + 1 - rangeL.location)];
        //排除“”空字符串
        if (![nextstr isEqualToString:@""]) {
            
            [array addObject:nextstr];
            
            NSString *str = [message substringFromIndex:rangeR.location + 1];
            [self getMessageRange:str :array];
        }
        else {
                
                return;
            }
        }
    }
    else {
        [array addObject:message];
    }
}

然后在这个数组中查找符合表情符号的字符,如果是你工程中匹配的表情符合就输出表情,否则输出字符串

- (NSAttributedString *)emojiStr {
    @try {
        
        NSMutableArray *imageArr = [NSMutableArray arrayWithCapacity:0];
        
        [self getMessageRange:self :imageArr];
        //NSLog(@"%@",imageArr);
        
        NSMutableAttributedString *mutableAttributedStr=[[NSMutableAttributedString alloc]initWithString:@""];
        [imageArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
  NSString *contentPartStr = [NSString stringWithFormat:@"%@",obj];
   if (contentPartStr.length > 0) {          
    NSString *firstC = [obj substringWithRange:NSMakeRange(0, 1)];              
   NSString *endC = [obj substringWithRange:NSMakeRange(1, contentPartStr.length-1)];
   NSAttributedString *attributedStr;
    if ([firstC isEqualToString:@"[" ]&&[endC rangeOfString:@"]" ].location!=NSNotFound ) {
      NSRange firstRange = [obj rangeOfString:@"["];
        NSRange secondRange = [obj rangeOfString:@"]"];              
   NSUInteger length = secondRange.location - firstRange.location;
   NSRange imageNameRange = NSMakeRange(1, length - 1);
  NSString *imageName = [obj substringWithRange:imageNameRange];
  NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
   attachment.image = [UIImage imageNamed:imageName];
   if (attachment.image==nil) {
 attributedStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"[%@]",imageName]];
  // NSLog(@"%@",imageName);
   }else{
            attachment.bounds = CGRectMake(0, 0, 20, 20);
            attributedStr = [[NSAttributedString alloc]initWithString:@""];
            attributedStr = [NSAttributedString attributedStringWithAttachment:attachment];
             }
}
                else{
attributedStr = [[NSAttributedString alloc]initWithString:obj];
                }
                [mutableAttributedStr appendAttributedString:attributedStr];
            }
        }];
         return mutableAttributedStr;
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
    @finally {
    }
}

代码中的@try@catch@finally的关键字是将可能引发异常的代码节放在 Try 块中,而将处理异常的代码放在 Catch 块中。Catch 块是一系列以关键字 catch 开头的语句,语句后跟异常类型和要执行的操作。异常发生时,执行将终止,并且控制交给最近的异常处理程序。这通常意味着不执行希望总是调用的代码行。有些资源清理(如关闭文件)必须总是执行,即使有异常发生。为实现这一点,可以使用 Finally 块。Finally 块总是执行,不论是否有异常发生。

本文中的方法不是很完善,像这两种表情字符串无法正常解析[[得意]],[字符串[得意]。
但是都做了处理程序不会崩溃,后期改正后再更新。

对含有表情字符串的解析,对textView的类别

思路一样只是封装的不一样。

 #import "UITextView+expression.h"

@implementation UITextView (expression)

//将每次输入的字段进行解析,如果有表情,则将表情解析完以后显示图片,没有则直接显示文字
- (void)setCommentTextView:(NSString *)content toTheview:(UILabel *) textView{
    self.text = @"";
    @try {
        
        NSMutableArray *imageArr = [NSMutableArray arrayWithCapacity:0];
        
        [self getMessageRange:content :imageArr];
        //NSLog(@"%@",imageArr);
        
        NSMutableAttributedString *mutableAttributedStr=[[NSMutableAttributedString alloc]initWithString:@""];
        [imageArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
         NSString *contentPartStr = [NSString stringWithFormat:@"%@",obj];
        if (contentPartStr.length > 0) {
                
         NSString *firstC = [obj substringWithRange:NSMakeRange(0, 1)];
                
           NSString *endC = [obj substringWithRange:NSMakeRange(1, contentPartStr.length-1)];
               NSAttributedString *attributedStr;
               if ([firstC isEqualToString:@"[" ]&&[endC rangeOfString:@"]" ].location!=NSNotFound ) {
                NSRange firstRange = [obj rangeOfString:@"["];
                NSRange secondRange = [obj rangeOfString:@"]"];
                NSUInteger length = secondRange.location - firstRange.location;
                NSRange imageNameRange = NSMakeRange(1, length - 1);
                NSString *imageName = [obj substringWithRange:imageNameRange];
                NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
                attachment.image = [UIImage imageNamed:imageName];
                if (attachment.image==nil) {
                    
                    
                    attributedStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"[%@]",imageName]];
                   // NSLog(@"%@",imageName);
        

                }else{
                    attachment.bounds = CGRectMake(0, 0, 20, 20);
                    attributedStr = [[NSAttributedString alloc]initWithString:@""];
                    attributedStr = [NSAttributedString attributedStringWithAttachment:attachment];
                }
            }
            else{
attributedStr = [[NSAttributedString alloc]initWithString:obj];
                
            }
            
            [mutableAttributedStr appendAttributedString:attributedStr];
                textView.attributedText =mutableAttributedStr;
        } 
    }];
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
    @finally {
    }
}

方法后面的toView可以改成textVew,UITextField有attributedText属性都可以,
如果是想在自身实现就将

 [mutableAttributedStr appendAttributedString:attributedStr];
                textView.attributedText =mutableAttributedStr;

改为

 [mutableAttributedStr appendAttributedString:attributedStr];
 [self.textStorage appendAttributedString:mutableAttributedStr];

OK封装完成可以用了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,505评论 18 399
  • (一)Java部分 1、列举出JAVA中6个比较常用的包【天威诚信面试题】 【参考答案】 java.lang;ja...
    独云阅读 7,030评论 0 62
  • 通俗编程——白话JAVA异常机制 - 代码之道,编程之法 - 博客频道 - CSDN.NEThttp://blog...
    葡萄喃喃呓语阅读 3,140评论 0 25
  • 没有遥远 没有近 影子的长度斜入寂无 只有这两棵树了 大部分叶子已经逃离 太高的地方 近也是遥远么 向上的都弯曲 ...
    杨昊田阅读 510评论 16 29