123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // EMTextViewController.m
- // ChatDemo-UI3.0
- //
- // Created by XieYajie on 2019/1/17.
- // Copyright © 2019 XieYajie. All rights reserved.
- //
- #import "EMTextViewController.h"
- #import "EMTextView.h"
- @interface EMTextViewController ()<UITextViewDelegate>
- @property (nonatomic, strong) NSString *originalString;
- @property (nonatomic, strong) NSString *placeholder;
- @property (nonatomic) BOOL isEditable;
- @property (nonatomic, strong) EMTextView *textView;
- @end
- @implementation EMTextViewController
- - (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(@120);
- }];
-
- self.textView = [[EMTextView alloc] init];
- self.textView.delegate = self;
- self.textView.backgroundColor = [UIColor clearColor];
- self.textView.font = [UIFont systemFontOfSize:16];
- self.textView.placeholder = self.placeholder;
- self.textView.returnKeyType = UIReturnKeyDone;
- self.textView.text = self.originalString;
- self.textView.editable = self.isEditable;
- [self.view addSubview:self.textView];
- [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(bgView);
- make.top.equalTo(bgView).offset(5);
- make.left.equalTo(bgView).offset(10);
- }];
- }
- #pragma mark - UITextViewDelegate
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- if ([text isEqualToString:@"\n"]) {
- [textView resignFirstResponder];
- return NO;
- }
-
- return YES;
- }
- #pragma mark - Action
- - (void)doneAction
- {
- [self.view endEditing:YES];
-
- BOOL isPop = YES;
- if (_doneCompletion) {
- isPop = _doneCompletion(self.textView.text);
- }
-
- if (isPop) {
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- @end
|