123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // EMTextFieldViewController.m
- // ChatDemo-UI3.0
- //
- // Created by XieYajie on 2019/1/16.
- // Copyright © 2019 XieYajie. All rights reserved.
- //
- #import "EMTextFieldViewController.h"
- @interface EMTextFieldViewController ()<UITextFieldDelegate>
- @property (nonatomic, strong) NSString *originalString;
- @property (nonatomic, strong) NSString *placeholder;
- @property (nonatomic) BOOL isEditable;
- @property (nonatomic, strong) UITextField *textField;
- @end
- @implementation EMTextFieldViewController
- - (instancetype)initWithString:(NSString *)aString
- placeholder:(NSString *)aPlaceholder
- isEditable:(BOOL)aIsEditable
- {
- self = [super init];
- if (self) {
- _originalString = aString;
- _placeholder = aPlaceholder;
- _isEditable = aIsEditable;
- }
-
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self _setupSubviews];
- }
- #pragma mark - Subviews
- - (void)_setupSubviews
- {
- [self addPopBackLeftItem];
- if (self.isEditable) {
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneAction)];
- }
-
- self.view.backgroundColor = kColor_LightGray;
- UIView *bgView = [[UIView alloc] init];
- bgView.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:bgView];
- [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.view).offset(20);
- make.left.equalTo(self.view);
- make.right.equalTo(self.view);
- make.height.equalTo(@60);
- }];
-
- self.textField = [[UITextField alloc] init];
- self.textField.delegate = self;
- self.textField.backgroundColor = [UIColor clearColor];
- self.textField.font = [UIFont systemFontOfSize:16];
- self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
- self.textField.returnKeyType = UIReturnKeyDone;
- self.textField.text = self.originalString;
- self.textField.enabled = self.isEditable;
- if (self.isEditable) {
- self.textField.placeholder = self.placeholder;
- }
- [self.view addSubview:self.textField];
- [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(bgView);
- make.top.equalTo(bgView).offset(5);
- make.left.equalTo(bgView).offset(10);
- }];
- }
- #pragma mark - UITextFieldDelegate
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- {
- if ([string isEqualToString:@"\n"]) {
- [textField resignFirstResponder];
- return NO;
- }
-
- return YES;
- }
- #pragma mark - Action
- - (void)doneAction
- {
- [self.view endEditing:YES];
-
- BOOL isPop = YES;
- if (_doneCompletion) {
- isPop = _doneCompletion(self.textField.text);
- }
-
- if (isPop) {
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- @end
|