1.控制器代码
#import "ViewController.h"
#import "NSAttributedString+Emoji.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel*labelText;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label=[[UILabel alloc]init];
label.frame=CGRectMake(10, 30, 300, 300);
NSString *str=@"大脸毅s012 s013 s014 s015dfadsfasdfs dfdf";
label.numberOfLines=0;
self.labelText=label;
//self.labelText.backgroundColor=[UIColor grayColor];
[self.view addSubview:label];
//特殊文本 加表情 方法一
//[self text1WithStr:str];
//自定义类递归实现 方法二
NSMutableAttributedString *attriM=[[NSMutableAttributedString alloc]initWithString:str];
NSAttributedString *result=[NSAttributedString emojiStringWithString:attriM];
self.labelText.attributedText=result;
}
-(void)text1WithStr:(NSString*)str
{
//属性字符串 只有文本的属性字符串
NSAttributedString *strm=[[NSAttributedString alloc]initWithString:str];
//创建一个带图片的附件对象
NSTextAttachment *atchment=[[NSTextAttachment alloc]init];
//给附件对象添加image;
atchment.image=[UIImage imageNamed:@"s012.png"];
NSAttributedString *strImage=[NSAttributedString attributedStringWithAttachment:atchment];
//用可变字符串来替换拼接附件文本和普通文本
NSMutableAttributedString *attributedStringM=[[NSMutableAttributedString alloc]initWithAttributedString:strm];
//正则表达式
NSString *pattren=@"[s][0-9]{3}";
NSRegularExpression *regular=[NSRegularExpression regularExpressionWithPattern:pattren options:1 error:NULL];
NSTextCheckingResult *result=[regular firstMatchInString:str options:1 range:NSMakeRange(0, str.length)];
[attributedStringM replaceCharactersInRange:result.range withAttributedString:strImage];
self.labelText.attributedText=attributedStringM;
}
@end
2.分类代码
#import <Foundation/Foundation.h>
@interface NSAttributedString (Emoji)
+ (NSAttributedString *)emojiStringWithString:(NSMutableAttributedString *)emojiString;
@end
#import "NSAttributedString+Emoji.h"
#import <UIKit/UIKit.h>
@interface EmojiAttachment : NSTextAttachment
@end
@implementation EmojiAttachment
//I want my emoticon has the same size with line's height
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
{
return CGRectMake( 0 , -3, lineFrag.size.height, lineFrag.size.height);
}
@end
@implementation NSAttributedString (Emoji)
+ (NSAttributedString *)emojiStringWithString:(NSMutableAttributedString *)emojiString
{
NSRegularExpression *regularEx = [NSRegularExpression regularExpressionWithPattern:@"s[0-9]{3}" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *string = emojiString.string;
NSTextCheckingResult *result = [regularEx firstMatchInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];
if (result != nil) {
NSString *imageName = [NSString stringWithFormat:@"%@.png", [string substringWithRange:result.range]];
EmojiAttachment *attachment = [[EmojiAttachment alloc] initWithData:nil ofType:nil];
attachment.image = [UIImage imageNamed:imageName];
NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
[emojiString replaceCharactersInRange:result.range withAttributedString:attrString];
// 递归
[self emojiStringWithString:emojiString];
} else {
return emojiString;
}
return emojiString;
}
@end