氣泡聊天

import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    //用盛放說話的內容
    NSMutableArray *_dataArray;
    //底部的視圖
    UIView *_bottomView;
    //輸入框
    UITextField *_textField;
    //判斷是誰說
    BOOL _who;
}
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] init];
    [self createBottomView];
    //separatorStyle 設置cell分割線的樣式
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //UIScrollViewKeyboardDismissModeOnDrag 當拖動的時候讓鍵盤隱藏
    _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}

#pragma mark - 創(chuàng)建底部的視圖
- (void)createBottomView
{
    //背景圖,輸入框,按鈕 都放在一個view之上,當鍵盤彈出的時候,改變view的frame即可
    //創(chuàng)建一個視圖
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 480-44, 320, 44)];
    [self.view addSubview:_bottomView];
    //添加背景圖
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    imgView.image = [UIImage imageNamed:@"bg"];
    [_bottomView addSubview:imgView];
    [imgView release];
    //添加輸入框
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(7, 7, 253, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    //設置輸入框的代理
    _textField.delegate = self;
    [_bottomView addSubview:_textField];
    //添加發(fā)送按鈕
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(260, 0, 60, 44);
    [btn setTitle:@"發(fā)送" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:btn];
}

#pragma mark - 發(fā)送按鈕點擊方法
- (void)sendMessage
{
    if ([_textField.text isEqualToString:@""])
    {
        //當內容為空的時候,不發(fā)送
        return;
    }
    //說的內容:誰、時間、說的話都在一個視圖之上,當你需要顯示內容的時候,讓cell  addSubview
    //當是YES:我,當是NO:靜靜
    _who = !_who;
    //1.獲取由誰說
    //如果條件為真,執(zhí)行結果1;如果條件為假,執(zhí)行結果2
    //三目運算符    條件?結果1:結果2
    NSString *name = _who?@"我":@"靜靜";
    
    //2.獲取說的時間
    //獲取當前時間
    NSDate *date = [NSDate date];
    //時間格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //設置時間格式
    formatter.dateFormat = @"MM-dd HH:mm:ss";
    //把時間轉化成字符串
    NSString *time = [formatter stringFromDate:date];

    //3.拼接所說的內容
    NSString *message = [NSString stringWithFormat:@"%@在%@說:\n %@",name,time,_textField.text];

    //4.獲取盛放內容的視圖
    UIView *view = [self createViewWithMessage:message];
    [_dataArray addObject:view];

    //5.刷新表
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArray.count-1 inSection:0];
    [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //當有新消息時,顯示最后一行
    //根據(jù)表的索引,移動到指定的位置
    //atScrollPosition 要移動的位置
    //UITableViewScrollPositionBottom 移動到底部
    [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

    //清空輸入框的內容
    _textField.text = @"";
}
//根據(jù)說的內容創(chuàng)建視圖
- (UIView *)createViewWithMessage:(NSString *)message
{
    //獲取文字內容寬高
    //參數(shù)1.文字的寬度200 和 高LONG_MAX
    //2.NSStringDrawingUsesLineFragmentOrigin根據(jù)行高計算文字的高度
    //3.attributes 文字的大小,和lab保持一致
    CGRect rect = [message boundingRectWithSize:CGSizeMake(200, LONG_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil];

    //獲取文字的高度
    float height = rect.size.height;
    float width = rect.size.width;
    //根據(jù)誰誰判斷x坐標
    float x = _who?self.view.frame.size.width-width-30:0;
    //1.創(chuàng)建view
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(x, 0, width+30, height+30)] autorelease];
    view.tag = 10;

    //2.添加氣泡
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, width+20, height+20)];
    //根據(jù)誰說獲取圖片名字
    NSString *imgName = _who?@"bubbleSelf":@"bubble";
    UIImage *img = [UIImage imageNamed:imgName];
    //stretchableImage拉伸圖片
    //LeftCapWidth距左邊多少開始拉伸
    //topCapHeight距上面多少開始拉伸
    //img.size.width 獲取圖片的寬
    imgView.image = [img stretchableImageWithLeftCapWidth:img.size.width*0.5 topCapHeight:img.size.height*0.5];
    [view addSubview:imgView];
    [imgView release];

    //3.添加lab顯示文字
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, width, height)];
    //要比顯示文字大小小一點
    lab.font = [UIFont systemFontOfSize:13];
    lab.numberOfLines = 0;
    lab.text = message;
    [view addSubview:lab];
    [lab release];

    return view;
}

#pragma mark - UITextFieldDelegate
//將要開始編輯的時候,鍵盤彈出,改變底部視圖和表的位置
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //添加動畫  216
    [UIView animateWithDuration:0.25 animations:^{
    //讓_bottomView上去
    _bottomView.frame = CGRectMake(0, 480-44-216, 320, 44);
    //讓表上去
    _tableView.frame = CGRectMake(0, 0, 320, 480-44-216);
    }];

    return YES;
}
//將要結束編輯,鍵盤下去,底部視圖和表也下去
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
    //視圖下去,回到初始位置
    _bottomView.frame = CGRectMake(0, 480-44, 320, 44);
    //改變表的位置
    _tableView.frame = CGRectMake(0, 0, 320, 480-44);
    }];
    
    return YES;
}
//點擊return讓鍵盤隱藏,失去第一響應
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //resignFirstResponder失去第一響應
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - 表的數(shù)據(jù)源和代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //先把添加的視圖移除,防止重用
    UIView *view10 = [cell viewWithTag:10];
    [view10 removeFromSuperview];

    //顯示要說的內容
    //從數(shù)組中獲取要說的內容
    UIView *view = [_dataArray objectAtIndex:indexPath.row];
    [cell addSubview:view];

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根據(jù)view的高度設置單元格的高度
    UIView *view = [_dataArray objectAtIndex:indexPath.row];
    return view.frame.size.height;
}

- (void)dealloc {
    [_dataArray release];
    [_textField release];
    [_bottomView release];
    [_tableView release];
    [super dealloc];
}
@end

附. UITextField 的代理方法
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"將要開始編輯");

    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"已經(jīng)開始編輯");
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"將要結束編輯");

    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"已經(jīng)結束編輯");
}
//改變輸入框內容的時候調用
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"我要開始寫啦");

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"點擊了return");
    //鍵盤彈出:成為第一響應becomeFirstResponder
    //鍵盤隱藏:失去第一響應resignFirstResponder
    //點擊return鍵讓鍵盤隱藏
    [textField resignFirstResponder];

    return YES;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容