zoukankan      html  css  js  c++  java
  • 微信[自动回复]

     
     

    #import "ViewController.h"

    #import "JWMessage.h"

    #import "JWChatCell.h"

    #import "JWMessageFrame.h"

    #import "UIImage+JWRsizeImage.h"

    @interfaceViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

    @property (strongnonatomicNSMutableArray *messageDatas;

    @property (weaknonatomicIBOutletUITableView *tableView;

    @property (weaknonatomicIBOutletUIView *toolbar;

    @property (weaknonatomicIBOutletUITextField *inputText;

    @property (strongnonatomicNSDictionary *replayDatas;

    @end

    @implementation ViewController

    -(NSDictionary *)replayDatas

    {

        if (!_replayDatas) {

            _replayDatas = [NSDictionarydictionaryWithContentsOfFile:[[NSBundlemainBundlepathForResource:@"repaly.plist"ofType:nil]];

        }

        return_replayDatas;

    }

    -(NSMutableArray *)messageDatas

    {

        if (!_messageDatas) {

            

            NSArray *allDatas = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundlepathForResource:@"messages.plist"ofType:nil]];

            NSMutableArray *tempArr = [NSMutableArrayarray];

            for (NSDictionary *dict in allDatas) {

                

                

                // 可以拿到上个message

                JWMessage *lastMessage = (JWMessage *)[[tempArr lastObjectmessage];

                

                JWMessage *message = [JWMessagemessageWithDict:dict];

                

                if ([lastMessage.timeisEqualToString:message.time]) {

                    message.hiddenTime = YES;

                }

                

                JWMessageFrame *messageFrame = [[JWMessageFramealloc]init];

                messageFrame.message = message; 

                

                [tempArr addObject:messageFrame];

            }

            _messageDatas = tempArr;

        }

        return_messageDatas;

    }

    - (void)viewDidLoad {

        [superviewDidLoad];

        self.tableView.backgroundColor = [UIColorcolorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];

        

        // 不允许点击选中

        self.tableView.allowsSelection = NO;

        

        // 注册通知

        // 键盘的frame改变的时候就会接受到通知,调用“self”“keyboardWillChangeFrame”

    //    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

        

        

        // 键盘将要显示的时候就会接受到通知

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:) name:    UIKeyboardWillShowNotificationobject:nil];

        // 键盘将要隐藏的时候就会接受到通知

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];

        

        

        // 设置textFieldleftView

        self.inputText.leftView = [[UIViewalloc]initWithFrame:CGRectMake(0, 0, 10, 0)];

        self.inputText.leftViewMode = UITextFieldViewModeAlways;

        

        self.inputText.delegate = self;

    }

    -(void)keyboardWillShow:(NSNotification *)noti

    {

        

        NSLog(@"%@",noti);

        // 拿到键盘弹出时间

        CGFloat showDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKeydoubleValue];

        

        // 动画弹出toolbar

        CGFloat keyboardHeight = [noti.userInfo[UIKeyboardFrameEndUserInfoKeyCGRectValue].size.height;

        

        

        // 动画上移toolbar

    //    [UIView animateWithDuration:showDuration animations:^{

    //    

    //        self.toolbar.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);

    //    }];

        

        

    /*

     UIKeyboardAnimationCurveUserInfoKey = 7;  弹出的快慢

     UIKeyboardAnimationDurationUserInfoKey = "0.25";  时间

     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 252}}";

     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 559}";

     UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 541}";

     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";

     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 415}, {375, 252}}";

     */

        

        

        

        

        [UIViewanimateWithDuration:showDuration delay:0.0 options:7 << 16 animations:^{

            self.toolbar.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);

            

        } completion:^(BOOL finished) {

            

        }];

        

    }

    -(void)keyboardWillHide:(NSNotification *)noti

    {

        CGFloat hiddenDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKeydoubleValue];

        

        // 动画还原位置

        [UIViewanimateWithDuration:hiddenDuration animations:^{

            self.toolbar.transform = CGAffineTransformIdentity;

        }];

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        returnself.messageDatas.count;

    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        JWChatCell *cell = [JWChatCellcellWithTableView:tableView];

        

        JWMessageFrame *messageFrame = self.messageDatas[indexPath.row];

        

        cell.messageFrame = messageFrame;

        

        return cell;

    }

    -(BOOL)prefersStatusBarHidden

    {

        returnYES;

    }

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        JWMessageFrame *messageFrame = self.messageDatas[indexPath.row];

        return messageFrame.cellHeight;

    }

    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

    {

        [self.viewendEditing:YES];

    }

    #pragma mark - UITextFiledDelegate

    -(BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        

        // 弄个数据,放到数组,tableView就显示出来你这条数据了

        [selfaddMessageWithString:textField.textwithType:JWMessageTypeMe];

        // 在弄一个数据,放到数组里

        

        NSString *replayStr = nil;

    //    钱是好东西

        for (int i = 0 ; i < textField.text.length; i ++) {

            

            NSRange range = NSMakeRange(i, 1);

            

            NSString *currentStr = [textField.textsubstringWithRange:range];

            NSLog(@"%@------",currentStr);

           

            for (NSString *key inself.replayDatas) {

                

                        NSLog(@"%@----------------",key);

                

                if ([key isEqualToString:currentStr]) {

                    replayStr = self.replayDatas[key];

                    break;

                }

            }

        }

        

        

        if (!replayStr) {

            replayStr = [NSStringstringWithFormat:@"%@?什么东西",textField.text];

        }

        

        [selfaddMessageWithString:replayStr withType:JWMessageTypeYou];

        

        

        

        // 刷新

        [self.tableViewreloadData];

        

        // 滚动tableView

        

        NSIndexPath *indexPath = [NSIndexPathindexPathForRow:self.messageDatas.count - 1 inSection:0];

        [self.tableViewscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottomanimated:YES];

        

        // 清空textField

        textField.text = nil;

        

        returnYES;

    }

    -(void)addMessageWithString:(NSString *)str withType:(JWMessageType)type

    {

        JWMessage *lastMessage = (JWMessage *)[[self.messageDataslastObjectmessage];

        

        

        JWMessage *message = [[JWMessagealloc]init];

        

        NSDate *now = [[NSDatealloc]init];

        

        NSDateFormatter *fm = [[NSDateFormatteralloc]init];

    //  HH:代表hour24小时制)

    //  hh:代表hour12小时制)

        

        fm.dateFormat = @"HH:mm";

        

        NSString *norStr = [fm stringFromDate:now];

        

        message.time = norStr;

        message.type = type;

        message.text = str;

        message.hiddenTime = [lastMessage.timeisEqualToString:message.time];

        

        JWMessageFrame *messageFrame = [[JWMessageFramealloc]init];

        messageFrame.message = message;

        

        [self.messageDatasaddObject:messageFrame];

        

    }

    @end

     
     
     

    #import "JWMessageFrame.h"

    #import <UIKit/UIKit.h>

    #import "NSString+JWSizeWithFont.h"

    @implementation JWMessageFrame

    -(void)setMessage:(JWMessage *)message

    {

        _message = message;

        

        CGFloat margin = 10;

        CGFloat screenW = [UIScreenmainScreen].bounds.size.width;

        

        // 计算子控件frame

        

        if(!message.hiddenTime)

        {

            CGFloat timeLabelX = 0;

            CGFloat timeLabelY = 0;

            CGFloat timeLabelW = screenW;

            CGFloat timeLabelH = 30;

            self.timeLabelF = CGRectMake(timeLabelX, timeLabelY, timeLabelW, timeLabelH);

        }

        CGFloat iconViewX;

        CGFloat iconViewW = 35;

        CGFloat iconViewH = iconViewW;

        

        if (JWMessageTypeMe == self.message.type) { // 我说的

            iconViewX = screenW - margin - iconViewW;

        }

        else

        {

            iconViewX = margin;

        }

        CGFloat iconViewY = CGRectGetMaxY(self.timeLabelF) + margin;

        

        self.iconViewF = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);

        CGFloat wordsBtnY = self.iconViewF.origin.y;

        CGSize wordsSize = [message.textsizeWithFont:[UIFontsystemFontOfSize:15] withMaxWidth:200];

        

        CGFloat wordsBtnW = wordsSize.width + kEdgeInsets * 2;

        CGFloat wordsBtnH = wordsSize.height + kEdgeInsets * 2;

        

        CGFloat wordsBtnX;

        if (JWMessageTypeMe == message.type) {

            wordsBtnX = screenW - margin * 2 - self.iconViewF.size.width - wordsBtnW;

        }

        else

        {

            wordsBtnX = margin * 2 + self.iconViewF.size.width;

        }

        self.wordsBtnF = CGRectMake(wordsBtnX, wordsBtnY, wordsBtnW, wordsBtnH);

        

        // cell高度

        self.cellHeight = CGRectGetMaxY(self.wordsBtnF);

        

    }

    @end

     
     
     
     
     

    #import "JWChatCell.h"

    #import "UIImage+JWRsizeImage.h"

    @interfaceJWChatCell ()

    @property (strongnonatomicUIImageView *iconView;

    @property (strongnonatomicUILabel *timeLabel;

    @property (strongnonatomicUIButton *wordsBtn;

    @end

    @implementation JWChatCell

    -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    {

        if (self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {

            // 添加子控件

            UILabel *timeLabel = [[UILabelalloc]init];

            timeLabel.textAlignment = NSTextAlignmentCenter;

            [self.contentViewaddSubview:timeLabel];

            timeLabel.textColor = [UIColorgrayColor];

            timeLabel.font = [UIFontsystemFontOfSize:13];

            self.timeLabel = timeLabel;

            

            UIImageView *iconView = [[UIImageViewalloc]init];

            [self.contentViewaddSubview:iconView];

            self.iconView = iconView;

            

            UIButton *wordsBtn = [[UIButtonalloc]init];

    //        wordsBtn.backgroundColor = [UIColor redColor];

            

            [self.contentViewaddSubview:wordsBtn];

            self.wordsBtn = wordsBtn;

            [wordsBtn setTitleColor:[UIColorblackColorforState:UIControlStateNormal];

            wordsBtn.titleLabel.font = [UIFontsystemFontOfSize:15];

            wordsBtn.titleLabel.numberOfLines = 0;

            wordsBtn.contentEdgeInsets = UIEdgeInsetsMake(kEdgeInsets , kEdgeInsetskEdgeInsetskEdgeInsets);

            

            

            

            

            self.backgroundColor = [UIColorclearColor];

           

            UIView *view = [[UIViewalloc]init];

    //        view.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];

    //        self.selectedBackgroundView = view;

        }

        returnself;

    }

    +(instancetype)cellWithTableView:(UITableView *)tableView

    {

        staticNSString *str = @"cell";

        JWChatCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

        if (!cell) {

            cell = [[JWChatCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:str];

        }

        

        return cell;

    }

    -(void)setMessageFrame:(JWMessageFrame *)messageFrame

    {

        _messageFrame = messageFrame;

        

        // 给子控件赋值

        self.timeLabel.text = messageFrame.message.time;

        

        if (JWMessageTypeMe == messageFrame.message.type) {

            

            self.iconView.image = [UIImageimageNamed:@"me"];

        }

        else

        {

            self.iconView.image = [UIImageimageNamed:@"other"];

        }

        

        [self.wordsBtnsetTitle:messageFrame.message.textforState:UIControlStateNormal];

        

        // 设置背景

        

        if (JWMessageTypeMe == messageFrame.message.type) {

            UIImage *norImage = [UIImageresizebleImageWithName:@"chat_send_nor"];

            [self.wordsBtnsetBackgroundImage:norImage forState:UIControlStateNormal];

        }

        else

        {

            UIImage *norImage = [UIImageresizebleImageWithName:@"chat_recive_nor"];

            [self.wordsBtnsetBackgroundImage:norImage forState:UIControlStateNormal];

        }

        

        // 给子控件设置frame

        self.timeLabel.frame = messageFrame.timeLabelF;

        self.iconView.frame = messageFrame.iconViewF;

        self.wordsBtn.frame = messageFrame.wordsBtnF;

    }

    @end

  • 相关阅读:
    uniapp 常用的基础知识
    uniapp接口请求(第一种)
    系统对象的使用——Cookie,ViewState,Session,Application
    网页跳转
    存储过程的使用——游标
    存储过程的使用——循环,条件语句
    存储过程的使用——定义变量
    页面读取英语单词
    加密
    XMind: ZEN 快捷键
  • 原文地址:https://www.cnblogs.com/GhostKZShadow/p/5108764.html
Copyright © 2011-2022 走看看