iOS聊天表情面板

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;

}];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,936評論 6 535
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,744評論 3 421
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,879評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,181評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,935評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,325評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,384評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,534評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,084評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,892評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,623評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,322評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,735評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,990評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,800評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,084評論 2 375

推薦閱讀更多精彩內(nèi)容