iOS聊天表情面板

1.scrollview +pagcontrol +imageview


/**

*  点击表情的选择按钮

*/

-(IBAction)faceSelect{

[moreView removeFromSuperview];

[self.inputTextView resignFirstResponder];

[speakbutton removeFromSuperview];

speakbutton =  nil;

if (!emotionScrollView) {

emotionScrollView  =  [[EmotionScrollView alloc]initWithFrame:CGRectMake(0, kDeviceHeight-KeyBoardHeight, kDeviceWidth*6, KeyBoardHeight)];

}

__weak  SXChatViewController    *sxchatView =  self;

[emotionScrollView.emotionView setSelectblock:^(UIImage *image, NSString*name){

//先获取到输入光标的位置

NSRange range  =  sxchatView.inputTextView.selectedRange;

NSMutableString *existenceString    =  [NSMutableString stringWithString:sxchatView.inputTextView.text];

[existenceString replaceCharactersInRange:range withString:name];

sxchatView.inputTextView.text      =  existenceString;

}];

[emotionScrollView setSendblock:^(){

[sxchatView sendMessage:sxchatView.inputTextView.text];

sxchatView.inputTextView.text  =  nil;

}];

[emotionScrollView.emotionView setDeleteblcok:^(){

sxchatView.inputTextView.attributedText =  nil;

}];

[self.view addSubview:emotionScrollView];

self.inputViewBottomConstraint.constant =  KeyBoardHeight;

[self.view updateConstraints];

[self.view layoutIfNeeded];

}



scrollview界面

//初始化界面

-(instancetype)initWithFrame:(CGRect)frame{

self    =  [super initWithFrame:frame];

if (self) {

emotionView  = [[EmotionView alloc]initWithFrame:CGRectMake(0,0,kDeviceWidth*PAGENUMBER,self.frame.size.height-BOTTOMHEIGHT-PAGEHEIGHT)];

_faceScrollView =  [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,kDeviceWidth,self.frame.size.height-BOTTOMHEIGHT-PAGEHEIGHT)];

[_faceScrollView setBackgroundColor:[UIColor clearColor]];

_faceScrollView.delegate    =  self;

_faceScrollView.pagingEnabled  =  YES;

_faceScrollView.contentSize    =  CGSizeMake(kDeviceWidth*PAGENUMBER, 0);

_faceScrollView.clipsToBounds  =  YES;

_faceScrollView.showsHorizontalScrollIndicator  =  YES;

_faceScrollView.showsVerticalScrollIndicator    =  YES;

[self addSubview:_faceScrollView];

[_faceScrollView addSubview:emotionView];

_facePageControl    =  [[UIPageControl alloc]initWithFrame:CGRectMake(kDeviceWidth/2-20, self.frame.size.height-BOTTOMHEIGHT-PAGEHEIGHT, BOTTOMHEIGHT, PAGEHEIGHT)];

[_facePageControl setBackgroundColor:[UIColor clearColor]];

_facePageControl.numberOfPages  =  PAGENUMBER;

_facePageControl.currentPage    =  0;

_facePageControl.pageIndicatorTintColor =  [UIColor grayColor];

_facePageControl.currentPageIndicatorTintColor  =  [UIColor blackColor];

[self addSubview:_facePageControl];

//发送按钮

UIButton *sendButton = [[UIButton alloc] initWithFrame:CGRectMake(kDeviceWidth-80,self.frame.size.height-30,80,30)];

[sendButton setBackgroundColor:HexRGB(SEABLUECOLOR)];

sendButton.titleLabel.font = [UIFont systemFontOfSize:15];

[sendButton setTitle:@"发送" forState:0];

[sendButton addTarget:self action:@selector(sendButtonPress:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:sendButton];

}

return self;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

NSInteger  pageNumber  =  scrollView.contentOffset.x/kDeviceWidth;

_facePageControl.currentPage    =  pageNumber;

}

-(void)sendButtonPress:(UIButton*)Button{

_sendblock();

}


表情view界面

//

//  EmotionView.m

//  Ebank

//

//  Created by MS_ios on 16/4/25.

//  Copyright © 2016年 bfy. All rights reserved.

//

#import "EmotionView.h"

#import "SvGifView.h"

#import "YiImageView.h"

#define item_width  (kDeviceWidth-220+140)/5

#define item_height 60

@interface EmotionView(){

NSMutableArray  *itemsArray;

}

@property  (nonatomic,strong)SvGifView *gifView;

@end

@implementation EmotionView

-(instancetype)initWithFrame:(CGRect)frame{

self    =  [super initWithFrame:frame];

if (self) {

[self setBackgroundColor:[UIColor clearColor]];

NSArray *faceArray  =  [NSArray arrayWithObjects:@"[呲牙]", @"[调皮]", @"[流汗]", @"[偷笑]", @"[再见]", @"[敲打]", @"[擦汗]", @"[猪头]", @"[玫瑰]", @"[流泪]", @"[大哭]", @"[嘘]", @"[酷]", @"[抓狂]", @"[委屈]", @"[便便]", @"[炸弹]", @"[菜刀]", @"[可爱]", @"[色]", @"[害羞]", @"[得意]", @"[吐]", @"[微笑]", @"[发怒]", @"[尴尬]", @"[惊恐]", @"[冷汗]", @"[爱心]", @"[示爱]", @"[白眼]", @"[傲慢]", @"[难过]", @"[惊讶]", @"[疑问]", @"[睡]", @"[亲亲]", @"[憨笑]", @"[爱情]", @"[衰]",  @"[撇嘴]", @"[阴险]", @"[奋斗]", @"[发呆]", @"[右哼哼]", @"[拥抱]", @"[坏笑]", @"[飞吻]", @"[鄙视]", @"[晕]", @"[大兵]", @"[可怜]", @"[强]", @"[弱]", @"[握手]", @"[胜利]", @"[抱拳]", @"[凋谢]", @"[饭]", @"[蛋糕]", @"[西瓜]", @"[啤酒]", @"[飘虫]", @"[勾引]", @"[OK]", @"[爱你]", @"[咖啡]", @"[钱]", @"[月亮]", @"[美女]", @"[刀]", @"[发抖]", @"[差劲]", @"[拳头]", @"[心碎]", @"[太阳]", @"[礼物]", @"[足球]", @"[骷髅]", @"[挥手]", @"[闪电]", @"[饥饿]", @"[困]", @"[咒骂]", @"[折磨]", @"[抠鼻]", @"[鼓掌]", @"[糗大了]", @"[左哼哼]", @"[哈欠]", @"[快哭了]", @"[吓]", @"[篮球]", @"[乒乓球]", @"[NO]", @"[跳跳]", @"[怄火]", @"[转圈]", @"[磕头]", @"[回头]", @"[跳绳]", @"[激动]", @"[街舞]", @"[献吻]", @"[左太极]", @"[右太极]", @"[闭嘴]",nil];

itemsArray  =  [NSMutableArray array];

NSMutableArray  *itemstwod      =  nil;

for (int i=0; i

NSDictionary    *item  =  [faceArray objectAtIndex:i];

if (i%17==0) {

itemstwod  =  [NSMutableArray arrayWithCapacity:17];

[itemsArray addObject:itemstwod];

}

[itemstwod addObject:item];

}

self.width  =  itemsArray.count *kDeviceWidth;

self.height =  3 * item_height;

}

return self;

}


-(void)drawRect:(CGRect)rect{

NSString  *filePath  =  [[NSBundle mainBundle]pathForResource:@"Emotion" ofType:@"plist"];

NSDictionary  *faceDictionary  =  [NSDictionary dictionaryWithContentsOfFile:filePath];

int row = 0,colum = 0;  //定义行、列

for (int i = 0; i < itemsArray.count; i++) {

NSArray *items2D = [itemsArray objectAtIndex:i];

for (int j = 0; j < items2D.count+1; j++) {

CGRect  emotionFrame  = CGRectMake(colum*item_width+20, row*item_height +15, 30  , 30 );

//考虑页数,需要加上前面一页的宽度

float x = (i*kDeviceWidth) + emotionFrame.origin.x;

emotionFrame.origin.x = x;

//为了增加删除发送按钮

if (j < items2D.count)

{

NSString *item = [items2D objectAtIndex:j];

YiImageView *imageView  =  [[YiImageView alloc]init];

imageView.imageName    =    item;

imageView.userInteractionEnabled    =  YES;

imageView.frame =  emotionFrame;

UITapGestureRecognizer  *tapgesture =  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTap:)];

[imageView addGestureRecognizer:tapgesture];

imageView.image =  [UIImage imageNamed:[faceDictionary objectForKey:item]];

[self addSubview:imageView];

//更新列、行

colum ++;

if (row==2) {

if (colum % 5 == 0) {

row ++;

colum = 0;

}

}else{

if (colum % 6 == 0) {

row ++;

colum = 0;

}

}

if (row % 3  == 0) {

row = 0;

}

}

else{

//删除按钮

UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(emotionFrame.origin.x+item_width*5-10, emotionFrame.origin.y+item_height*2, 30, 30)];

[deleteButton setImage:[UIImage imageNamed:@"faceDelete"] forState:0];

[deleteButton addTarget:self action:@selector(deleteButtonPress:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:deleteButton];

}

//最后一行加一个删除按钮

if (i==itemsArray.count-1&&j==5) {

//删除按钮

UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(25+6*kDeviceWidth+item_width*5-10, 15, 30, 30)];

[deleteButton setImage:[UIImage imageNamed:@"faceDelete"] forState:0];

[deleteButton addTarget:self action:@selector(deleteButtonPress:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:deleteButton];

break;

}

}

}

}

/**

*  图片选中方法

*

*  @param tapgesture 单点手势。。

*/

-(void)imageViewTap:(UITapGestureRecognizer*)tapgesture{

YiImageView *imageView  =  (YiImageView*)tapgesture.view;

_selectblock(imageView.image,imageView.imageName);

}

/**

*  删除按钮点击方法

*

*  @param button 删除按钮。。

*/

-(void)deleteButtonPress:(UIButton*)button{

_deleteblcok();

}


#pragma mark 表情的回调处理

在CELL上显示表情图片

//显示表情,用属性字符串显示表情 

-(NSMutableAttributedString *)showFace:(NSString *)str

{

if (str != nil) {

NSArray *faceArray  =  [NSArray arrayWithObjects:@"[呲牙]", @"[调皮]", @"[流汗]", @"[偷笑]", @"[再见]", @"[敲打]", @"[擦汗]", @"[猪头]", @"[玫瑰]", @"[流泪]", @"[大哭]", @"[嘘]", @"[酷]", @"[抓狂]", @"[委屈]", @"[便便]", @"[炸弹]", @"[菜刀]", @"[可爱]", @"[色]", @"[dele1]", @"[害羞]", @"[得意]", @"[吐]", @"[微笑]", @"[发怒]", @"[尴尬]", @"[惊恐]", @"[冷汗]", @"[爱心]", @"[示爱]", @"[白眼]", @"[傲慢]", @"[难过]", @"[惊讶]", @"[疑问]", @"[睡]", @"[亲亲]", @"[憨笑]", @"[爱情]", @"[衰]", @"[dele2]", @"[撇嘴]", @"[阴险]", @"[奋斗]", @"[发呆]", @"[右哼哼]", @"[拥抱]", @"[坏笑]", @"[飞吻]", @"[鄙视]", @"[晕]", @"[大兵]", @"[可怜]", @"[强]", @"[弱]", @"[握手]", @"[胜利]", @"[抱拳]", @"[凋谢]", @"[饭]", @"[蛋糕]", @"[dele3]", @"[西瓜]", @"[啤酒]", @"[飘虫]", @"[勾引]", @"[OK]", @"[爱你]", @"[咖啡]", @"[钱]", @"[月亮]", @"[美女]", @"[刀]", @"[发抖]", @"[差劲]", @"[拳头]", @"[心碎]", @"[太阳]", @"[礼物]", @"[足球]", @"[骷髅]", @"[挥手]", @"[dele4]", @"[闪电]", @"[饥饿]", @"[困]", @"[咒骂]", @"[折磨]", @"[抠鼻]", @"[鼓掌]", @"[糗大了]", @"[左哼哼]", @"[哈欠]", @"[快哭了]", @"[吓]", @"[篮球]", @"[乒乓球]", @"[NO]", @"[跳跳]", @"[怄火]", @"[转圈]", @"[磕头]", @"[回头]", @"[dele5]", @"[跳绳]", @"[激动]", @"[街舞]", @"[献吻]", @"[左太极]", @"[右太极]", @"[闭嘴]", nil];

//加载plist文件中的数据

NSBundle *bundle = [NSBundle mainBundle];

//寻找资源的路径

NSString *path = [bundle pathForResource:@"Emotion" ofType:@"plist"];

//获取plist中的数据

NSDictionary *faceDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];

//创建一个可变的属性字符串

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str];

UIFont *baseFont = [UIFont systemFontOfSize:17];

[attributeString addAttribute:NSFontAttributeName value:baseFont

range:NSMakeRange(0, str.length)];

//正则匹配要替换的文字的范围

//正则表达式

NSString * pattern = @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]";

NSError *error = nil;

NSRegularExpression * re = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

if (!re) {

NSLog(@"%@", [error localizedDescription]);

}

//通过正则表达式来匹配字符串

NSArray *resultArray = [re matchesInString:str options:0 range:NSMakeRange(0, str.length)];

//用来存放字典,字典中存储的是图片和图片对应的位置

NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:resultArray.count];

//根据匹配范围来用图片进行相应的替换

for(NSTextCheckingResult *match in resultArray) {

//获取数组元素中得到range

NSRange range = [match range];

//获取原字符串中对应的值

NSString *subStr = [str substringWithRange:range];

for (int i = 0; i < faceArray.count; i ++)

{

if ([faceArray[i] isEqualToString:subStr])

{

//  新建文字附件来存放我们的图片

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];

//给附件添加图片

textAttachment.image = [[UIImage imageNamed:[faceDictionary objectForKey:subStr]] scaleImageWithWidth:30.f];

//把附件转换成可变字符串,用于替换掉源字符串中的表情文字

NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];

//把图片和图片对应的位置存入字典中

NSMutableDictionary *imageDic = [NSMutableDictionary dictionaryWithCapacity:2];

[imageDic setObject:imageStr forKey:@"image"];

[imageDic setObject:[NSValue valueWithRange:range] forKey:@"range"];

//把字典存入数组中

[imageArray addObject:imageDic];

}

}

}

//从后往前替换

for (int i = (int)imageArray.count -1; i >= 0; i--)

{

NSRange range;

[imageArray[i][@"range"] getValue:&range];

//进行替换

[attributeString replaceCharactersInRange:range withAttributedString:imageArray[i][@"image"]];

}

return  attributeString;

}

return nil;

}

回调文本显示表情的处理

__weak  SXChatViewController    *sxchatView =  self;

[emotionScrollView.emotionView setSelectblock:^(UIImage *image, NSString*name){

//先获取到输入光标的位置

NSRange range  =  sxchatView.inputTextView.selectedRange;

NSMutableString *existenceString    =  [NSMutableString stringWithString:sxchatView.inputTextView.text];

[existenceString replaceCharactersInRange:range withString:name];

sxchatView.inputTextView.text      =  existenceString;

}];

你可能感兴趣的:(iOS聊天表情面板)