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

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

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


得意@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封装完成可以用了。

你可能感兴趣的:(对含有表情字符的字符串类别的封装)