chating
- 隐藏相同时间的label
- (NSArray *)messages
{
if (_messages == nil) {
// 加载plist中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 字典数组 -> 模型数组
NSMutableArray *messageArray = [NSMutableArray array];
// 用来记录上一条消息模型
XMGMessage *lastMessage = nil;
for (NSDictionary *dict in dictArray) {
XMGMessage *message = [XMGMessage messageWithDict:dict];
//埋下伏笔,加载数据时,判断哪个时间值相等。
message.hideTime = [message.time isEqualToString:lastMessage.time];
[messageArray addObject:message];
lastMessage = message;
}
_messages = messageArray;
}
return _messages;
}
- 图片拉伸
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(10, 10, 250, 150);
imageView.image = [UIImage imageNamed:@"chat_send_nor"];
[self.view addSubview:imageView];
// 加载原图
// UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
// 拉伸处理(说明需要保护的区域)
// image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(30, 30, 30, 30) resizingMode:UIImageResizingModeStretch];
// image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
设置按钮
// 设置按钮的文字 [showTextButton setTitle:self.message.text forState:UIControlStateNormal]; // 强制更新 [showTextButton layoutIfNeeded]; // 设置按钮的高度就是titleLabel的高度 [showTextButton updateConstraints:^(MASConstraintMaker *make) { CGFloat buttonH = showTextButton.titleLabel.frame.size.height; make.height.equalTo(buttonH); }];
- 设置field文本框左边
// 设置文本框左边的内容 UIView *leftView = [[UIView alloc] init]; leftView.frame = CGRectMake(0, 0, 10, 0); self.messageField.leftView = leftView; self.messageField.leftViewMode = UITextFieldViewModeAlways;
- 设置field文本框左边
键盘处理
double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改约束
self.bottomSpacing.constant = rect.size.height;
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardWillHide:(NSNotification *)note {
// 取出键盘弹出需要花费的时间
double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改约束
self.bottomSpacing.constant = 0;
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
- 退出键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 退出键盘
// [self.messageField resignFirstResponder];
// [self.messageField endEditing:YES];
[self.view endEditing:YES];
}
另一种方式
// 监听键盘通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; - (void)keyboardWillChangeFrame:(NSNotification *)note { // 取出键盘最终的frame CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 取出键盘弹出需要花费的时间 double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改约束 self.bottomSpacing.constant = [UIScreen mainScreen].bounds.size.height - rect.origin.y; [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }]; }
一定要移除监听通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}