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)建復(fù)制粘貼即可鸵闪。當然有什么問題,可以留言給我雕欺,O(∩_∩)O謝謝岛马!