一、气泡聊天
1.滚动到最后一行
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
2.获取文字占据的矩形空间
Size:限制矩形块的宽/高
options:NSStringDrawingUsesLineFragmentOrigin 每一行文字所占据的矩形空间
attributes:设置文字的属性
context:上下文(一般为nil)
CGRect messageRect = [message boundingRectWithSize:CGSizeMake(200, LONG_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
3.图片拉伸
(LeftCapWidth,topCapHeight) 该点一般位于图片中心点附近
UIImage *newImg = [oldImg stretchableImageWithLeftCapWidth:22 topCapHeight:16];
4.设置聊天头像(图片剪裁)
selfCell.selfHeader.image = [UIImage imageNamed:@"0.jpg"];
selfCell.selfHeader.layer.cornerRadius = 20;
selfCell.selfHeader.clipsToBounds = YES;
5.单元格选中样式
selfCell.selectionStyle = UITableViewCellSelectionStyleNone;
6.给tableView加上手势,触发键盘消失
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[_tableView addGestureRecognizer:gestureRecognizer];
- (void)hideKeyboard
{
[_messageTf resignFirstResponder];
}
二、移除监听者
文件:ViewController.m
- (void)dealloc
{
//如果在自定义内部,将该类的对象注册为监听者
//若对象被释放,必须把监听者从通知中心移除
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"吃饭" object:nil];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//1.如果将某个VC(viewController)注册为Observer,当VC被释放的时候,系统会自动把Observer(VC)从通知中心移除掉
// NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// [center addObserver:self selector:@selector(changeAction) name:@"xxx" object:nil];
People *people = [[People alloc]init];
[people release];
}
文件:People.m
- (id)init
{
self = [super init];
if (self)
{
//在自定义类内部注册了一个监听者,
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(eat) name:@"吃饭" object:nil];
}
return self;
}
自定义ViewController NavigationController