EMTextFieldViewController.m 3.0 KB

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