1.scrollview +pagcontrol +imageview
/**
*? 點(diǎn)擊表情的選擇按鈕
*/
-(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){
//先獲取到輸入光標(biāo)的位置
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];
//發(fā)送按鈕
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:@"發(fā)送" 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:@"[呲牙]", @"[調(diào)皮]", @"[流汗]", @"[偷笑]", @"[再見]", @"[敲打]", @"[擦汗]", @"[豬頭]", @"[玫瑰]", @"[流淚]", @"[大哭]", @"[噓]", @"[酷]", @"[抓狂]", @"[委屈]", @"[便便]", @"[炸彈]", @"[菜刀]", @"[可愛]", @"[色]", @"[害羞]", @"[得意]", @"[吐]", @"[微笑]", @"[發(fā)怒]", @"[尷尬]", @"[驚恐]", @"[冷汗]", @"[愛心]", @"[示愛]", @"[白眼]", @"[傲慢]", @"[難過]", @"[驚訝]", @"[疑問]", @"[睡]", @"[親親]", @"[憨笑]", @"[愛情]", @"[衰]",? @"[撇嘴]", @"[陰險]", @"[奮斗]", @"[發(fā)呆]", @"[右哼哼]", @"[擁抱]", @"[壞笑]", @"[飛吻]", @"[鄙視]", @"[暈]", @"[大兵]", @"[可憐]", @"[強(qiáng)]", @"[弱]", @"[握手]", @"[勝利]", @"[抱拳]", @"[凋謝]", @"[飯]", @"[蛋糕]", @"[西瓜]", @"[啤酒]", @"[飄蟲]", @"[勾引]", @"[OK]", @"[愛你]", @"[咖啡]", @"[錢]", @"[月亮]", @"[美女]", @"[刀]", @"[發(fā)抖]", @"[差勁]", @"[拳頭]", @"[心碎]", @"[太陽]", @"[禮物]", @"[足球]", @"[骷髏]", @"[揮手]", @"[閃電]", @"[饑餓]", @"[困]", @"[咒罵]", @"[折磨]", @"[摳鼻]", @"[鼓掌]", @"[糗大了]", @"[左哼哼]", @"[哈欠]", @"[快哭了]", @"[嚇]", @"[籃球]", @"[乒乓球]", @"[NO]", @"[跳跳]", @"[慪火]", @"[轉(zhuǎn)圈]", @"[磕頭]", @"[回頭]", @"[跳繩]", @"[激動]", @"[街舞]", @"[獻(xiàn)吻]", @"[左太極]", @"[右太極]", @"[閉嘴]",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 );
//考慮頁數(shù),需要加上前面一頁的寬度
float x = (i*kDeviceWidth) + emotionFrame.origin.x;
emotionFrame.origin.x = x;
//為了增加刪除發(fā)送按鈕
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 單點(diǎn)手勢。。
*/
-(void)imageViewTap:(UITapGestureRecognizer*)tapgesture{
YiImageView *imageView? =? (YiImageView*)tapgesture.view;
_selectblock(imageView.image,imageView.imageName);
}
/**
*? 刪除按鈕點(diǎn)擊方法
*
*? @param button 刪除按鈕。。
*/
-(void)deleteButtonPress:(UIButton*)button{
_deleteblcok();
}
#pragma mark 表情的回調(diào)處理
在CELL上顯示表情圖片
//顯示表情,用屬性字符串顯示表情?
-(NSMutableAttributedString *)showFace:(NSString *)str
{
if (str != nil) {
NSArray *faceArray? =? [NSArray arrayWithObjects:@"[呲牙]", @"[調(diào)皮]", @"[流汗]", @"[偷笑]", @"[再見]", @"[敲打]", @"[擦汗]", @"[豬頭]", @"[玫瑰]", @"[流淚]", @"[大哭]", @"[噓]", @"[酷]", @"[抓狂]", @"[委屈]", @"[便便]", @"[炸彈]", @"[菜刀]", @"[可愛]", @"[色]", @"[dele1]", @"[害羞]", @"[得意]", @"[吐]", @"[微笑]", @"[發(fā)怒]", @"[尷尬]", @"[驚恐]", @"[冷汗]", @"[愛心]", @"[示愛]", @"[白眼]", @"[傲慢]", @"[難過]", @"[驚訝]", @"[疑問]", @"[睡]", @"[親親]", @"[憨笑]", @"[愛情]", @"[衰]", @"[dele2]", @"[撇嘴]", @"[陰險]", @"[奮斗]", @"[發(fā)呆]", @"[右哼哼]", @"[擁抱]", @"[壞笑]", @"[飛吻]", @"[鄙視]", @"[暈]", @"[大兵]", @"[可憐]", @"[強(qiáng)]", @"[弱]", @"[握手]", @"[勝利]", @"[抱拳]", @"[凋謝]", @"[飯]", @"[蛋糕]", @"[dele3]", @"[西瓜]", @"[啤酒]", @"[飄蟲]", @"[勾引]", @"[OK]", @"[愛你]", @"[咖啡]", @"[錢]", @"[月亮]", @"[美女]", @"[刀]", @"[發(fā)抖]", @"[差勁]", @"[拳頭]", @"[心碎]", @"[太陽]", @"[禮物]", @"[足球]", @"[骷髏]", @"[揮手]", @"[dele4]", @"[閃電]", @"[饑餓]", @"[困]", @"[咒罵]", @"[折磨]", @"[摳鼻]", @"[鼓掌]", @"[糗大了]", @"[左哼哼]", @"[哈欠]", @"[快哭了]", @"[嚇]", @"[籃球]", @"[乒乓球]", @"[NO]", @"[跳跳]", @"[慪火]", @"[轉(zhuǎn)圈]", @"[磕頭]", @"[回頭]", @"[dele5]", @"[跳繩]", @"[激動]", @"[街舞]", @"[獻(xiàn)吻]", @"[左太極]", @"[右太極]", @"[閉嘴]", nil];
//加載plist文件中的數(shù)據(jù)
NSBundle *bundle = [NSBundle mainBundle];
//尋找資源的路徑
NSString *path = [bundle pathForResource:@"Emotion" ofType:@"plist"];
//獲取plist中的數(shù)據(jù)
NSDictionary *faceDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
//創(chuàng)建一個可變的屬性字符串
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str];
UIFont *baseFont = [UIFont systemFontOfSize:17];
[attributeString addAttribute:NSFontAttributeName value:baseFont
range:NSMakeRange(0, str.length)];
//正則匹配要替換的文字的范圍
//正則表達(dá)式
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]);
}
//通過正則表達(dá)式來匹配字符串
NSArray *resultArray = [re matchesInString:str options:0 range:NSMakeRange(0, str.length)];
//用來存放字典,字典中存儲的是圖片和圖片對應(yīng)的位置
NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:resultArray.count];
//根據(jù)匹配范圍來用圖片進(jìn)行相應(yīng)的替換
for(NSTextCheckingResult *match in resultArray) {
//獲取數(shù)組元素中得到range
NSRange range = [match range];
//獲取原字符串中對應(yīng)的值
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];
//把附件轉(zhuǎn)換成可變字符串,用于替換掉源字符串中的表情文字
NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
//把圖片和圖片對應(yīng)的位置存入字典中
NSMutableDictionary *imageDic = [NSMutableDictionary dictionaryWithCapacity:2];
[imageDic setObject:imageStr forKey:@"image"];
[imageDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
//把字典存入數(shù)組中
[imageArray addObject:imageDic];
}
}
}
//從后往前替換
for (int i = (int)imageArray.count -1; i >= 0; i--)
{
NSRange range;
[imageArray[i][@"range"] getValue:&range];
//進(jìn)行替換
[attributeString replaceCharactersInRange:range withAttributedString:imageArray[i][@"image"]];
}
return? attributeString;
}
return nil;
}
回調(diào)文本顯示表情的處理
__weak? SXChatViewController? ? *sxchatView =? self;
[emotionScrollView.emotionView setSelectblock:^(UIImage *image, NSString*name){
//先獲取到輸入光標(biāo)的位置
NSRange range? =? sxchatView.inputTextView.selectedRange;
NSMutableString *existenceString? ? =? [NSMutableString stringWithString:sxchatView.inputTextView.text];
[existenceString replaceCharactersInRange:range withString:name];
sxchatView.inputTextView.text? ? ? =? existenceString;
}];