美文网首页iOS Developer
自定义UITextView(OC)

自定义UITextView(OC)

作者: 村长大人tardis_cxx | 来源:发表于2017-05-10 11:48 被阅读125次

    iOS UITextView 属性


    有这样一种需求,在UITextView的属性里是没有像UITextFiled一样的占位文字的,那么要让UITextView拥有占位文字,需要什么思路呢?

    解决方案如下:

    • 分析:如同UITextField一样,当没有输入时显示占位文字,当有输入时随即消失,当输入完再删除时,又立刻出现
    • 由上分析,可以肯定,这个占位文字我选择一个UILable控件,因为它能够显示文字,而且便于修改占位文字属性
    • 通过分析,用通知或者代理来监听UITextView属性的改变

    具体代码如下:

    OC代码

    @interface TCTextView () <UITextViewDelegate>
    
    @property (nonatomic, strong) UILabel *placeholderLabel;
    
    @end
    
    @implementation TCTextView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            [self setupUI];
        }
        return self;
    }
    
    #pragma mark - UI 
    
    - (void)setupUI {
        [self addSubview:self.textView];
        [self.textView addSubview:self.placeholderLabel];
        
        // 此处使用了Masonry自动布局,可以替换为计算frame或者其他
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self);
            make.left.equalTo(self).offset(18.0);
            make.right.equalTo(self).offset(-18.0);
        }];
        
        [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.left.equalTo(self.textView).offset(8.0);
        }];
    
    }
    
    #pragma mark - public
    
    - (void)setupTextViewBorderColor:(UIColor *)color borderWidth:(CGFloat)bordWidth {
        if (color != nil) {
            self.textView.layer.borderColor = color.CGColor;
        }
        
        if (bordWidth >= 0) {
            self.textView.layer.borderWidth = bordWidth;
        }
    }
    
    #pragma mark - setter
    
    - (void)setPlaceholderLabelText:(NSString *)placeholderLabelText {
        if ([_placeholderLabelText isEqualToString:placeholderLabelText]) {
            return;
        }
        _placeholderLabelText = placeholderLabelText;
        self.placeholderLabel.text = _placeholderLabelText;
    }
    
    
    
    #pragma mark - UITextViewDelegate
    
    - (void)textViewDidBeginEditing:(UITextView *)textView {
        self.textView.layer.borderColor = COLOR_RGB(29, 169, 158).CGColor;
    }
    
    - (void)textViewDidChange:(UITextView *)textView {
        self.placeholderLabel.text = @"";
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView {
        if (textView.text.length > 0) {
            self.placeholderLabel.text = @"";
            return;
        }
        self.placeholderLabel.text = self.placeholderLabelText ?: @"请输入内容(限制50字)";
    }
    
    //- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    //    if ([text isEqualToString:@"\n"]) {
    //        [self.textView endEditing:YES];
    //        
    //        return NO;
    //    }
    //    return YES;
    //}
    
    //- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    //    
    //}
    
    
    #pragma mark - getter
    
    - (UITextView *)textView {
        if (!_textView) {
            _textView = [[UITextView alloc] init];
            _textView.font = [UIFont systemFontOfSize:20.0];
            _textView.layer.borderColor = COLOR_RGB(224, 224, 224).CGColor;
            _textView.layer.borderWidth = 1.0;
            _textView.layer.cornerRadius = 4.0;
            _textView.delegate = self;
            _textView.textColor = COLOR_RGB(69, 69, 69);
        }
        return _textView;
    }
    
    - (UILabel *)placeholderLabel {
        if (!_placeholderLabel) {
            _placeholderLabel = [UILabel tc_createLabelWithTitle:@"请输入内容(限制50字)" font:20.0 color:COLOR_RGB(135, 135, 135) alignment:NSTextAlignmentLeft];
            _placeholderLabel.backgroundColor = [UIColor clearColor];
        }
        return _placeholderLabel;
    }
    

    写在最后

    TCTextView是继承自UITextView的自定义子类,如果使用,直接创建复制粘贴即可。当然有什么问题,可以留言给我,O(∩_∩)O谢谢!

    相关文章

      网友评论

        本文标题:自定义UITextView(OC)

        本文链接:https://www.haomeiwen.com/subject/kaowtxtx.html