国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

XMPP鍵盤訂制實現(xiàn)圖文混排

2019-11-14 18:36:52
字體:
供稿:網(wǎng)友

  在現(xiàn)階段的通信服務(wù)中,各種標(biāo)準(zhǔn)都有,因此會出現(xiàn)無法實現(xiàn)相互連通,而XMPP(Extensible Message and PResence Protocol)協(xié)議的出現(xiàn),實現(xiàn)了整個及時通信服務(wù)協(xié)議的互通。有了這個協(xié)議之后,使用任何一個組織或者個人提供的即使通信服務(wù),都能夠無障礙的與其他的及時通信服務(wù)的用戶進(jìn)行交流。例如google 公司2005年推出的Google talk就是一款基于XMPP協(xié)議的即時通信軟件。在前面的系列博文中,我們介紹了XMPP的詳細(xì)使用(查看系列文章:http://www.survivalescaperooms.com/jerehedu/p/4607599.html),下面我們就談?wù)撘幌氯绾魏唵蔚氖褂肵MPP的鍵盤訂制:

  1、首先增加鍵盤的自定義小圖標(biāo)和彈出效果

  效果圖如下:

#pragma mark  - 排列按鈕- (void) setUpSubviews{        //1 初始化圖片名稱    NSArray* array=@[@"compose_camerabutton_background_os7",@"compose_toolbar_picture_os7",@"compose_mentionbutton_background_os7",@"compose_trendbutton_background_os7",@"compose_emoticonbutton_background_os7"];        //2 排列按鈕    CGFloat space=(kWidth-kMargin*2-kItemNum*kItemWidth)/(kItemNum-1)+kItemWidth;        for (int i=0; i<kItemNum; i++) {                UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];        button.tag=i;        button.frame=CGRectMake(kMargin+i*space, self.frame.size.height/2.0-kItemHeight/2.0, kItemWidth, kItemHeight);        //        button.backgroundColor=JRRandomColor();        [button setBackgroundImage:[UIImage imageNamed:array[i]] forState:UIControlStateNormal];                //Actions 按鈕事件        [button addTarget:self action:@selector(btClick:) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:button];    }  }/增加鍵盤事件彈出通知監(jiān)控    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyUp:) name:UIKeyboardWillShowNotification object:nil];        //增加鍵盤事件消失通知監(jiān)控[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyDown:) name:UIKeyboardWillHideNotification object:nil];#pragma mark - 鍵盤升起來- (void) keyUp:(NSNotification *) notification{        //獲取動畫的時間    CGFloat animaTime=[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];        //獲取鍵盤的尺寸(用來確定動畫的范圍)    CGRect frame=[notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];        //控制鍵盤動畫    [UIView animateWithDuration:animaTime animations:^{        self.keyaccess.transform=CGAffineTransformMakeTranslation(0, frame.size.height*-1);    }];            }#pragma mark - 鍵盤落下去- (void) keyDown:(NSNotification *) notification{        //獲取動畫的時間    CGFloat animaTime=[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];        [UIView animateWithDuration:animaTime animations:^{        self.keyAccess.transform=CGAffineTransformIdentity;    }];    }

  2、定義自定義鍵盤的圖標(biāo)

  表情主要分為三塊默認(rèn),Emoji,浪小花,默認(rèn)和浪小花為圖標(biāo),而Emoji為字符,因此我們需要進(jìn)行特殊處理。效果圖如下:

  代碼如下:

  1、我們首先自定義鍵盤視圖,同時我們需要把表情抽取出來因此還需要自定義一個滾動表情視圖

  //增加滾動表情        [self setUpSrollEmotion];                //增加自定義的tab        [self setUpTab];                //設(shè)置默認(rèn)第一個        if (self.btArray.count>1) {            [self clickBt:self.btArray[1]];            self.emotionScroll.emotionArray=self.defaultArray;        }else{            [self clickBt:[self.btArray firstObject]];        }pragma mark - 自定義鍵盤布局#pragma mark 設(shè)置滾動表情- (void) setUpSrollEmotion{    JRScrollEmotion * scroll=[[JRScrollEmotion alloc] initWithFrame:CGRectMake(0, 0, kWidth, self.frame.size.height-44)];    self.emotionScroll=scroll;        scroll.emotionArray=nil;    [self addSubview:scroll];        }#pragma mark 增加tab- (void) setUpTab{        UIView * bgview=[[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-44, kWidth, 44)];    bgview.backgroundColor=JRColor(109, 109, 109);    [self addSubview:bgview];        //計算按鈕寬度    CGFloat width=kWidth/4.0;    //標(biāo)題數(shù)組    NSArray * array=@[@"最近",@"默認(rèn)",@"Emoji",@"浪小花"];        for (int i=0 ; i<4; i++) {        UIButton * button=[[UIButton alloc] initWithFrame:CGRectMake(i*width, 0, width, 44)];        button.tag=i;        [button setTitle:array[i] forState:UIControlStateNormal];        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];        [button addTarget:self action:@selector(clickBt:) forControlEvents:UIControlEventTouchUpInside];        [self.btArray addObject:button];        [bgview addSubview:button];            }        }

  2、第二步我們需要進(jìn)行對表情進(jìn)行循環(huán)布局,每個表情作為一個button,我們進(jìn)行循環(huán)擺放

-(void)setEmotionArray:(NSArray *)emotionArray{    _emotionArray=emotionArray;        //移除所有button    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];        //計算總頁數(shù)    NSInteger totalPage=ceil(self.emotionArray.count/23.0);    self.contentSize=CGSizeMake(totalPage*kWidth, 0);    CGFloat hspace=(kWidth-40-8*35)/7.0+35;    CGFloat vspace=(self.frame.size.height-20-3*35)/2.0+35;        for (int i=0; i<self.emotionArray.count; i++) {        NSInteger nowPage=[self getNowPageWith:i];                NSInteger col=(i-nowPage*23)%8;        NSInteger row=(i-nowPage*23)/8;                UIButton *button=[[UIButton alloc ] initWithFrame:CGRectMake(nowPage*kWidth+20+col*hspace, 10+row*vspace, 35, 35)];                        //根據(jù)表類型設(shè)置圖片        JREmotionModel * model=self.emotionArray[i];                if (model.imageName==nil) {//emoji表情            [button setTitle:model.emoji forState:UIControlStateNormal];            button.titleLabel.font=[UIFont systemFontOfSize:35];        }else{          [button setImage:[UIImage imageNamed:model.imageName] forState:UIControlStateNormal];        }                //監(jiān)控點擊事件        button.tag=i;        [button addTarget:self action:@selector(emotionClick:) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:button];            }               for (int i=0; i<totalPage; i++) {                //增加刪除按鈕        UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];                if (i<totalPage-1) {            button.frame=CGRectMake(kWidth-35-20+i*kWidth, vspace*2+10, 35, 35);        }else{            //獲取剩下的個數(shù)            NSInteger numLeft= self.emotionArray.count-i*23;            NSInteger row=(numLeft)/8;            NSInteger col=(numLeft)%8;            button.frame=CGRectMake(i*kWidth+20+hspace*col, 10+vspace*row, 35, 35);        }                //====        [button setImage:[UIImage imageNamed:@"compose_emotion_delete_highlighted"] forState:UIControlStateNormal];                [button addTarget:self action:@selector(emotionDelete) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:button];        }      }

  3、我們需要進(jìn)行圖文混排將信息展示到文本框

    ①當(dāng)點擊表情的時候我們需要發(fā)送通知,告訴接受者

#pragma mark - 表情點解- (void) emotionClick:(UIButton *) button{    //獲取對應(yīng)的表情模型    JREmotionModel *model=self.emotionArray[button.tag];        //發(fā)送通知    [[NSNotificationCenter defaultCenter] postNotificationName:AddEmotionNotification object:nil userInfo:@{@"emotion":model}];

    ②接收到通知后通過富文本技術(shù)進(jìn)行顯示

- (void)addEmotion:(NSNotification * ) notification{       JREmotionModel * model= notification.userInfo[@"emotion"];               //如果是Emoji表情直接插入文本即可    if (model.imageName.length==0) {        [self.tf insertText:model.emoji];    }else{        //獲取之前的文本        NSAttributedString * text=self.tf.attributedText;                //將之前的文本包含進(jìn)去        NSMutableAttributedString * attr=[[NSMutableAttributedString alloc] initWithAttributedString:text];                //記錄當(dāng)前的位置        NSInteger index;                //如果是圖片表情,需要重新初始化一個附件,并設(shè)置圖片然后拼接        JRTextAttachMent * temAttch=[[JRTextAttachMent alloc] init];        temAttch.model=model;        temAttch.bounds=CGRectMake(0, -2.5, self.tf.font.lineHeight-5, self.tf.font.lineHeight-5);        temAttch.image=[UIImage imageNamed:model.imageName];                NSAttributedString * tempStr=[NSAttributedString attributedStringWithAttachment:temAttch];                //保存一下之前的位置        index=self.tf.selectedRange.location;        [attr insertAttributedString:tempStr atIndex:index];                //重新給文本框賦值        [attr addAttribute:NSFontAttributeName value:self.tf.font range:NSMakeRange(0, attr.length)];        self.tf.attributedText=attr;        self.tf.selectedRange=NSMakeRange(index+1, 0);    }     }

 

  想要了解更多內(nèi)容的小伙伴,可以點擊查看源碼,親自運行測試。

  疑問咨詢或技術(shù)交流,請加入官方QQ群:JRedu技術(shù)交流 (452379712)

 

作者:杰瑞教育
出處:http://www.survivalescaperooms.com/jerehedu/ 
本文版權(quán)歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 台中市| 石城县| 太仆寺旗| 安顺市| 建德市| 通榆县| 武平县| 宜宾县| 宣汉县| 台山市| 三门峡市| 崇明县| 光山县| 原平市| 家居| 利津县| 申扎县| 应用必备| 盐池县| 通道| 花垣县| 金沙县| 德昌县| 郯城县| 荥阳市| 崇义县| 文水县| 肃北| 应用必备| 宁明县| 镇雄县| 新巴尔虎左旗| 济源市| 通海县| 磐石市| 弋阳县| 淮北市| 阿拉善左旗| 山东省| 大港区| 大关县|