气泡聊天 -- 类似短信、微信界面(计算输入框输入的内容的尺寸size)

//***********************************************************// 

   *                               气泡聊天 

//***********************************************************// 

气泡聊天使用 : UITableview+UIView(放输入框)

 

 

 

保持气泡图片不失真的原理:是将一张规则的图片,选择它的一行一列的像素,根据内容大小对选择的这些像素进行复制。这样就可以保证,气泡图片不失真。

 

关键代码:

 

      UIImage *leftImage=[UIImage imageNamed:@"ReceiverTextNodeBkg.png"];

    UIImage *rightImage=[UIImage imageNamed:@"SenderTextNodeBkg.png"];

    

    //设定一行一列像素进行复制,LeftCapWidth表示行,topCapHeight表是列,它们都是指的是:从图片的零零点位置开始算起的

    leftImage=[leftImage stretchableImageWithLeftCapWidth:30 topCapHeight:40];

    rightImage=[rightImage stretchableImageWithLeftCapWidth:30 topCapHeight:40];

 

 因为点击输入框会弹出键盘。例如:微信,短信等,都是弹出键盘后,输入框自动向上移动。

//***********************************************************// 

   *                               输入框,按下键盘,收起 

//***********************************************************// 

 

利用UIApplication的监听机制

 

    //监听键盘出现

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];

    

    //监听键盘消失

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

 

//键盘将要出现

-(void)keyboardWillAppear:(NSNotification *)noti

{

    //获取键盘的size

    CGSize size=[noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

 

//改变tableview和view的frame

    _tableView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-40-size.height);

    _chatView.frame=CGRectMake(0,self.view.frame.size.height-45-size.height, self.view.frame.size.width, 45);

    

}

//键盘将要隐藏

-(void)keyboardWillHide:(NSNotification *)noti

{

//恢复tableview和view的frame

    _tableView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-40);

    _chatView.frame=CGRectMake(0,self.view.frame.size.height-45, self.view.frame.size.width, 45);

}

//***********************************************************// 

   *                               计算输入框输入的内容的尺寸size

//***********************************************************// 

关键代码:

//获取文本的size

    CGSize size = [item.content boundingRectWithSize:CGSizeMake(250, 1000) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]} context:nil].size;

 

 

以下是工程代码:

1:工程文件目录截图:

2.工程代码:

   2.1:数据model

   AZChatItem:

1
2
3
4
5
6
#import <Foundation/Foundation.h>
 
@interface  AZChatItem :  NSObject
@property  ( nonatomic ,assign) BOOL  isSelf; //表示是否是自己所发,因为气泡会更加这个来判断显示哪一个视图
@property  ( nonatomic , copy ) NSString  *content; //发送的消息
@end

   2.2:视图View

   AZChatTableViewCell:

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import <UIKit/UIKit.h>
#import "AZChatItem.h"
@interface  AZChatTableViewCell : UITableViewCell
@property  ( nonatomic ,strong)UIImageView *leftImageView;
@property  ( nonatomic ,strong)UIImageView *rightIamgeView;
@property  ( nonatomic ,strong)UILabel *leftLabel;
@property  ( nonatomic ,strong)UILabel *rightLabel;
 
//对外提供接口
 
//创建一个cell,带有复用
+(AZChatTableViewCell *)cellWithTableView:(UITableView *)tableView reuseIdentifier:( NSString  *)reuseIdentifier;
 
//根据model修改cell子视图控件
-( void )setFrameByModel:(AZChatItem *)chatItem;
 
@end

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#import "AZChatTableViewCell.h"
 
@implementation  AZChatTableViewCell
 
- ( id )initWithStyle:(UITableViewCellStyle)style reuseIdentifier:( NSString  *)reuseIdentifier
{
     self 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值