InputView.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // InputView.m
  3. // TableViewDemo
  4. //
  5. // Created by BenGang on 14-7-21.
  6. // Copyright (c) 2014年 BenGang. All rights reserved.
  7. //
  8. #import "InputView.h"
  9. #define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
  10. #define ORINGIN_X(view) view.frame.origin.x
  11. #define ORINGIN_Y(view) view.frame.origin.y
  12. #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
  13. #define VIEW_HEIGHT(view) view.frame.size.height
  14. @implementation InputView
  15. - (id)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. // Initialization code
  20. }
  21. [_inputTextView setFont:[UIFont fontWithName:@"BauhausITC" size:17.0]];
  22. return self;
  23. }
  24. - (void)awakeFromNib
  25. {
  26. self.inputTextView.layer.borderColor = [ RGB(200, 200, 200) CGColor];
  27. self.images.backgroundColor = RGB(245, 245, 245);
  28. [_inputTextView setFrame:CGRectMake(ORINGIN_X(_inputTextView), ORINGIN_Y(_inputTextView), SCREEN_WIDTH-80, VIEW_HEIGHT(_inputTextView))];
  29. // [self.inputTextView setRight:SCREEN_WIDTH-60];
  30. // [self.inputTextView setLeft:0];
  31. // [self.inputTextView setRight:(SCREEN_WIDTH - VIEW_WIDTH(_publishButton))];
  32. self.inputTextView.layer.borderWidth = 1.0;
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
  35. }
  36. - (void)keyboardWillShowNotification:(NSNotification *)notification
  37. {
  38. /*
  39. NSDictionary *userInfo = [notification userInfo];
  40. CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  41. NSValue *animationDuration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  42. NSTimeInterval timeInterval = 0;
  43. [animationDuration getValue:&timeInterval];
  44. if ([self.delegate respondsToSelector:@selector(keyboardWillShow:keyboardHeight:animationDuration:)]) {
  45. [self.delegate keyboardWillShow:self keyboardHeight:keyboardFrame.size.height animationDuration:timeInterval];
  46. }
  47. */
  48. CGRect keyboardEndFrameWindow;
  49. [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardEndFrameWindow];
  50. double keyboardTransitionDuration;
  51. [[notification.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&keyboardTransitionDuration];
  52. UIViewAnimationCurve keyboardTransitionAnimationCurve;
  53. [[notification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&keyboardTransitionAnimationCurve];
  54. // CGRect keyboardEndFrameView = [self convertRect:keyboardEndFrameWindow fromView:nil];
  55. // 参数 :速度 高度,时间
  56. if ([self.delegate respondsToSelector:@selector(keyboardWillShow:keyboardHeight:animationDuration:animationCurve:)]) {
  57. [self.delegate keyboardWillShow:self keyboardHeight:keyboardEndFrameWindow.size.height animationDuration:keyboardTransitionDuration animationCurve:keyboardTransitionAnimationCurve];
  58. }
  59. }
  60. - (void)keyboardWillHideNotification:(NSNotification *)notification
  61. {
  62. /*
  63. NSDictionary *userInfo = [notification userInfo];
  64. CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  65. NSValue *animationDuration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  66. NSTimeInterval timeInterval = 0;
  67. [animationDuration getValue:&timeInterval];
  68. if ([self.delegate respondsToSelector:@selector(keyboardWillHide:keyboardHeight:animationDuration:)]) {
  69. [self.delegate keyboardWillHide:self keyboardHeight:keyboardFrame.size.height animationDuration:timeInterval];
  70. }
  71. */
  72. CGRect keyboardEndFrameWindow;
  73. [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardEndFrameWindow];
  74. double keyboardTransitionDuration;// 获取键盘的速度
  75. [[notification.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&keyboardTransitionDuration];
  76. UIViewAnimationCurve keyboardTransitionAnimationCurve;
  77. [[notification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&keyboardTransitionAnimationCurve];
  78. if ([self.delegate respondsToSelector:@selector(keyboardWillHide:keyboardHeight:animationDuration:animationCurve:)]) {
  79. [self.delegate keyboardWillHide:self keyboardHeight:keyboardEndFrameWindow.size.height animationDuration:keyboardTransitionDuration animationCurve:keyboardTransitionAnimationCurve];
  80. }
  81. }
  82. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  83. {
  84. NSMutableString *times = [[NSMutableString alloc]initWithFormat:@"%@", textView.text];
  85. //字符串查找,可以判断字符串中是否有
  86. if ([times hasPrefix:@"@"]) {
  87. textView.text = @"";
  88. // textView.textColor = RGB(70, 70, 70);
  89. }
  90. return YES;
  91. }
  92. - (void)textViewDidChange:(UITextView *)textView
  93. {
  94. //计算文本的高度
  95. CGSize constraintSize = CGSizeMake(textView.frame.size.width-16, 60);
  96. CGRect sizeFrame = CGRectZero;
  97. NSDictionary *attributes = @{NSFontAttributeName:textView.font};
  98. NSInteger options = NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin;
  99. sizeFrame = [textView.text boundingRectWithSize:constraintSize options:options attributes:attributes context:NULL];
  100. sizeFrame.size.height += textView.font.lineHeight;
  101. textView.height = sizeFrame.size.height;
  102. //重新调整textView的高度
  103. if ([self.delegate respondsToSelector:@selector(textViewHeightDidChange:)]) {
  104. [self.delegate textViewHeightDidChange:textView.size.height];
  105. }
  106. }
  107. - (IBAction)recordButtonClick:(id)sender {
  108. if ([self.delegate respondsToSelector:@selector(recordButtonDidClick:)]) {
  109. [self.delegate recordButtonDidClick:sender];
  110. }
  111. }
  112. - (IBAction)addButtonClick:(id)sender {
  113. if ([self.delegate respondsToSelector:@selector(addButtonDidClick:)]) {
  114. [self.delegate addButtonDidClick:sender];
  115. }
  116. }
  117. - (IBAction)publishButtonClick:(id)sender {
  118. if ([self.delegate respondsToSelector:@selector(publishButtonDidClick:)]) {
  119. [self.delegate publishButtonDidClick:sender];
  120. }
  121. }
  122. - (void)resetInputView
  123. {
  124. self.height = 44;
  125. self.inputTextView.height = 30;
  126. [self setNeedsLayout];
  127. }
  128. - (void)layoutSubviews
  129. {
  130. [super layoutSubviews];
  131. self.addButton.top = self.height/2 - self.addButton.height/2;
  132. self.recordButton.top = self.height/2 - self.recordButton.height/2;
  133. self.publishButton.top = self.height/2 - self.publishButton.height/2;
  134. }
  135. - (void)dealloc
  136. {
  137. [[NSNotificationCenter defaultCenter] removeObserver:self];
  138. }
  139. @end