SendMessageView.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // SendMessageView.m
  3. // IBOSS
  4. //
  5. // Created by guan hong hou on 16/1/18.
  6. // Copyright © 2016年 elongtian. All rights reserved.
  7. //
  8. #import "SendMessageView.h"
  9. @interface SendMessageView () <UITextFieldDelegate>
  10. @property (nonatomic, strong) UITextField *inputTextField;
  11. @property (nonatomic, strong) UIButton *sendBtn;
  12. @property (nonatomic)CGFloat offset;
  13. @end
  14. @implementation SendMessageView
  15. #pragma mark -公有方法
  16. //初始化发送消息布局
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. [[NSNotificationCenter defaultCenter] addObserver:self
  21. selector:@selector(keyboardWillShow:)
  22. name:UIKeyboardWillShowNotification
  23. object:nil];
  24. }
  25. return self;
  26. }
  27. #pragma mark -委托回调方法
  28. //输入框回车事件
  29. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  30. if ([self.sendMessageDelegate respondsToSelector:@selector(didSendMessage:albumInputView:)]) {
  31. [self.sendMessageDelegate didSendMessage:textField.text albumInputView:self];
  32. }
  33. return YES;
  34. }
  35. #pragma mark -私有方法
  36. //初始化发送消息视图
  37. -(void)setHintMessage:(NSString*)hintMessage view:(UIView*) view{
  38. CGRect inputTextRect=CGRectMake(10, 13, CGRectGetWidth([[UIScreen mainScreen] bounds]) - 60,24);
  39. if (!_inputTextField) {
  40. _inputTextField = [[UITextField alloc] initWithFrame:inputTextRect];
  41. _inputTextField.placeholder=hintMessage;
  42. _inputTextField.delegate = self;
  43. _inputTextField.returnKeyType = UIReturnKeySend;
  44. _inputTextField.borderStyle = UITextBorderStyleRoundedRect;
  45. [self addSubview:_inputTextField];
  46. [_inputTextField becomeFirstResponder];
  47. }
  48. if(!_sendBtn){
  49. _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  50. [_sendBtn addTarget:self action:@selector(sendData)
  51. forControlEvents:UIControlEventTouchUpInside];
  52. _sendBtn.frame = CGRectMake(CGRectGetMaxX(inputTextRect)+3,13,40, 24);
  53. [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
  54. [_sendBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  55. [self addSubview:_sendBtn];
  56. }
  57. }
  58. //键盘弹出通知
  59. -(void)keyboardWillShow:(NSNotification *)note
  60. {
  61. CGFloat kbHeight = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
  62. //设置高度,根据父控件的高度-键盘高度-输入框的高度,在外围已经将父控件的高度设置成到屏幕底部
  63. self.frame = CGRectMake(0.0f,self.superview.frame.size.height-kbHeight-50, self.frame.size.width,self.frame.size.height);
  64. }
  65. //隐藏键盘
  66. - (void)finishSendMessage {
  67. self.inputTextField.text = nil;
  68. [ self.inputTextField resignFirstResponder];
  69. }
  70. //发送消息方法
  71. -(void)sendData{
  72. NSString *sendInfo=_inputTextField.text;
  73. [self.sendMessageDelegate didSendMessage:sendInfo albumInputView:self];
  74. }
  75. @end