一娜遵、設(shè)置PlaceHolder
1.聲明屬性和代理
<UITextViewDelegate>
@property(nonatomic,strong)UILabel *PLlab;
@property(nonatomic,strong)UITextView *inputView;
2.創(chuàng)建textview和lable
//輸入框視圖
inputView = [[UITextView alloc]initWithFrame:CGRectMake(5, 72, WIDTH-10, (HEIGHT-70)/3-20)];
inputView.keyboardType = UIKeyboardTypeDefault;
inputView.font = [UIFont systemFontOfSize:14];
inputView.delegate = self;
inputView.textColor = TEXTCOLOR;
[self.view addSubview:inputView];
// [self.inputView setValue:[UIColor colorWithHex:0xbababa] forKeyPath:@"_placeholderLabel.textColor"];
// [self.inputView setValue:TEXTFONT(16) forKeyPath:@"_placeholderLabel.font"];
self.PLlab = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, WIDTH, 40)];
self.PLlab.text = @"您此刻的想法,最多1500字";
self.PLlab.font = [UIFont systemFontOfSize:14];
self.PLlab.textColor = TEXTTINTCOLOR;
[inputView addSubview:self.PLlab];
3.開始編輯時調(diào)用相應(yīng)方法隱藏lable
- (void)textViewDidBeginEditing:(UITextView *)textView{
self.PLlab.hidden = YES;
}
4.點擊view縮回鍵盤時顯示lable
- (void)touchesBegan:(NSSet<UITouch> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
if (inputView.text.length > 0) {
self.PLlab.hidden = YES;
}else{
self.PLlab.hidden = NO;
}
}
二、注意點
1.在輸入框的textViewDidChange代理方法里面,如果直接調(diào)用textView.text或者textView.attributedText會導(dǎo)致蘋果簡體鍵盤輸入不了英文州叠;需要調(diào)用UITextRange和UITextPosition后众,在賦值attributedText才能解決;
2.輸入框允許輸入emoji后祟昭,并且調(diào)用了subStringToIndex方法缕坎,會導(dǎo)致表情被截斷,出現(xiàn)一個帶框的A的異常符號篡悟,提交網(wǎng)絡(luò)請求會崩潰谜叹;解決方案有兩種:一種是把帶emoji的文字進(jìn)行轉(zhuǎn)碼匾寝;一種是遍歷文字中的字符,將表情算作一個字符長度荷腊;
3.textFiled是一個道理艳悔;
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者