EMTextViewController.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // EMTextViewController.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/17.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMTextViewController.h"
  9. #import "EMTextView.h"
  10. @interface EMTextViewController ()<UITextViewDelegate>
  11. @property (nonatomic, strong) NSString *originalString;
  12. @property (nonatomic, strong) NSString *placeholder;
  13. @property (nonatomic) BOOL isEditable;
  14. @property (nonatomic, strong) EMTextView *textView;
  15. @end
  16. @implementation EMTextViewController
  17. - (instancetype)initWithString:(NSString *)aString
  18. placeholder:(NSString *)aPlaceholder
  19. isEditable:(BOOL)aIsEditable
  20. {
  21. self = [super init];
  22. if (self) {
  23. _originalString = aString;
  24. _placeholder = aPlaceholder;
  25. _isEditable = aIsEditable;
  26. }
  27. return self;
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // Do any additional setup after loading the view.
  32. [self _setupSubviews];
  33. }
  34. #pragma mark - Subviews
  35. - (void)_setupSubviews
  36. {
  37. [self addPopBackLeftItem];
  38. if (self.isEditable) {
  39. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneAction)];
  40. }
  41. self.view.backgroundColor = kColor_LightGray;
  42. UIView *bgView = [[UIView alloc] init];
  43. bgView.backgroundColor = [UIColor whiteColor];
  44. [self.view addSubview:bgView];
  45. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.top.equalTo(self.view).offset(20);
  47. make.left.equalTo(self.view);
  48. make.right.equalTo(self.view);
  49. make.height.equalTo(@120);
  50. }];
  51. self.textView = [[EMTextView alloc] init];
  52. self.textView.delegate = self;
  53. self.textView.backgroundColor = [UIColor clearColor];
  54. self.textView.font = [UIFont systemFontOfSize:16];
  55. self.textView.placeholder = self.placeholder;
  56. self.textView.returnKeyType = UIReturnKeyDone;
  57. self.textView.text = self.originalString;
  58. self.textView.editable = self.isEditable;
  59. [self.view addSubview:self.textView];
  60. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.center.equalTo(bgView);
  62. make.top.equalTo(bgView).offset(5);
  63. make.left.equalTo(bgView).offset(10);
  64. }];
  65. }
  66. #pragma mark - UITextViewDelegate
  67. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  68. {
  69. if ([text isEqualToString:@"\n"]) {
  70. [textView resignFirstResponder];
  71. return NO;
  72. }
  73. return YES;
  74. }
  75. #pragma mark - Action
  76. - (void)doneAction
  77. {
  78. [self.view endEditing:YES];
  79. BOOL isPop = YES;
  80. if (_doneCompletion) {
  81. isPop = _doneCompletion(self.textView.text);
  82. }
  83. if (isPop) {
  84. [self.navigationController popViewControllerAnimated:YES];
  85. }
  86. }
  87. @end