iOS 表情功能

参考文章
IOS开发微博中涉及到的一些正则表达式(表情,话题,链接,@)
将表情字符串转化成对应的emoji表情图片
正则表达式

// 需要正则的字符串
    NSString *str = @"#呵呵呵#[偷笑] http://test.com/blah_blah";
 // 表情的规则
    NSString *emotionPattern = @"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]";
    
    // @的规则
    NSString *atPattern = @"@[0-9a-zA-Z\\u4e00-\\u9fa5]+";
    
    // #话题#的规则
    NSString *topicPattern = @"#[0-9a-zA-Z\\u4e00-\\u9fa5]+#";
    
    // url链接的规则
    NSString *urlPattern = @"\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))";
    
    // | 匹配多个条件,相当于or\或
    NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@", emotionPattern, atPattern, topicPattern, urlPattern];
    // 使用系统的正则类来遍历
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    // 2.测试字符串
    NSArray *results = [regex matchesInString:str options:0 range:NSMakeRange(0, str.length)];
    

一、添加NSAttributedString的分类NSAttributedString+Emoji
NSAttributedString+Emoji.h文件

+ (NSAttributedString *)emojiStringWithString:(NSMutableAttributedString *)emojiString;

NSAttributedString+Emoji.m文件

+ (NSAttributedString *)emojiStringWithString:(NSMutableAttributedString *)emojiString

{

    NSRegularExpression *regularEx = [NSRegularExpression regularExpressionWithPattern:@"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]" options:NSRegularExpressionCaseInsensitive error:nil];

    NSString *string = emojiString.string;

    NSTextCheckingResult *result = [regularEx firstMatchInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

    if (result != nil) {
        NSDictionary *allPicDict = [SySkinManager sharedSkinManager].faceMap;       
        NSDictionary *dict = allPicDict[@"key1"];

        NSString *imageNameKey = [string substringWithRange:result.range];
        NSString *imageName = [dict objectForKey:imageNameKey];

        for (NSString *key in dict.allKeys) {
            NSString *value= [dict objectForKey:key];
            if ([value isEqualToString:imageNameKey]) {
                imageName = key;
            }
        }
        
        EmojiAttachment *attachment = [[EmojiAttachment alloc] initWithData:nil ofType:nil];
        UIImage *image = [UIImage imageNamed:imageName];
        attachment.image = image;
        attachment.bounds = CGRectMake(0, 0, image.size.width + 30, image.size.height);
        NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];

        [emojiString replaceCharactersInRange:result.range withAttributedString:attrString];

        // 递归

        [self emojiStringWithString:emojiString];

    } else {

        return emojiString;

    }

    return emojiString;

}

添加NSTextAttachment的子类EmojiAttachment

EmojiAttachment.m

- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex

{
// 调整一下图片的位置,如果你的图片偏上或者偏下,调整一下bounds的y值即可
    return CGRectMake( 0 , -3, lineFrag.size.height, lineFrag.size.height);

}

你可能感兴趣的:(iOS 表情功能)