自定義UITextView(OC)

iOS UITextView 屬性


有這樣一種需求,在UITextView的屬性里是沒有像UITextFiled一樣的占位文字的,那么要讓UITextView擁有占位文字,需要什么思路呢?

解決方案如下:

  • 分析:如同UITextField一樣,當沒有輸入時顯示占位文字,當有輸入時隨即消失,當輸入完再刪除時,又立刻出現(xiàn)
  • 由上分析,可以肯定,這個占位文字我選擇一個UILable控件,因為它能夠顯示文字,而且便于修改占位文字屬性
  • 通過分析,用通知或者代理來監(jiān)聽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 ?: @"請輸入內(nèi)容(限制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:@"請輸入內(nèi)容(限制50字)" font:20.0 color:COLOR_RGB(135, 135, 135) alignment:NSTextAlignmentLeft];
        _placeholderLabel.backgroundColor = [UIColor clearColor];
    }
    return _placeholderLabel;
}

寫在最后

TCTextView是繼承自UITextView的自定義子類,如果使用,直接創(chuàng)建復制粘貼即可。當然有什么問題,可以留言給我,O(∩_∩)O謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容