本文只講述UITextView的一些常見問題以及對應(yīng)的解決辦法辕录,廢話不說直接開講:
一昵仅、給UITextView設(shè)置一個placeholder括授,最簡單的方法通過Runtime獲取私有屬性設(shè)置,當(dāng)然你不嫌麻煩還可以通過代理方法或者給textView加label等方法實現(xiàn)岩饼。
UILabel *placeHolderLabel = [[UILabel alloc] init];
placeHolderLabel.text = @"請輸入內(nèi)容";
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.textColor = [UIColor lightGrayColor];
// 必須要設(shè)置字體,否則會導(dǎo)致label位置不準(zhǔn)確
placeHolderLabel.font = textView.font;
[textView addSubview:placeHolderLabel];
// 使用runtime賦值給私有屬性
[textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
二薛夜、UITextView內(nèi)邊距問題
UITextView默認(rèn)文本與邊框之間會有間距籍茧,文本距離邊距距離為8,如果你不想要或者想修改這個值梯澜,一句話搞定
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
三寞冯、設(shè)置行間距和字間距
// 行間距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5;
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
NSParagraphStyleAttributeName:paragraphStyle,
NSKernAttributeName:@10 // 字間距
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
四、光標(biāo)大小修改
自定義textView晚伙,重寫 - (CGRect)caretRectForPosition:(UITextPosition *)position 返回光標(biāo)大小和位置即可
- (CGRect)caretRectForPosition:(UITextPosition *)position {
CGRect originalRect = [super caretRectForPosition:position];
originalRect.size.height = self.font.lineHeight + 2;
return originalRect;
}
五吮龄、富文本樣式消失問題
如果給textView設(shè)置了富文本,一旦富文本被全部刪除了咆疗,再輸入文字便恢復(fù)成普通文本樣式了漓帚,就不再有富文本的樣式了。為了解決這個問題午磁,我是在textView的代理方法 - (void)textViewDidChange:(UITextView *)textView 中檢測文本重新修改樣式(如果只是字體和顏色這樣的可以通過設(shè)置UITextView的屬性來達(dá)到的尝抖,就沒必要這么麻煩了。但是需要設(shè)置行間距字間距或者實時解析項目里的表情迅皇,就可能需要這么做了)
- (void)textViewDidChange:(UITextView *)textView
{
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:textView.text];
[attString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
NSForegroundColorAttributeName:[UIColor redColor]
}
range:NSMakeRange(0, attString.length)];
textView.attributedText = attString;
}
六昧辽、系統(tǒng)中文輸入法問題
問題五中的解決方式存在一個問題,就是當(dāng)鍵盤為系統(tǒng)默認(rèn)中文鍵盤時登颓,當(dāng)你輸入一個字母時搅荞,輸入框中的內(nèi)容會出現(xiàn)問題,以下截圖是我分別輸入了 g h 后的顯示結(jié)果上面出現(xiàn)的情況就是蘋果會將英文作為備選文字先填入輸入框中框咙,解決辦法就是如果輸入框有備選文字就不進(jìn)行富文本處理咕痛,將問題五修改后如下:
- (void)textViewDidChange:(UITextView *)textView
{
UITextRange *range = [textView markedTextRange];
UITextPosition *position = [textView positionFromPosition:range.start offset:0];
if (!position) {
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:textView.text];
[attString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
NSForegroundColorAttributeName:[UIColor redColor]
}
range:NSMakeRange(0, attString.length)];
textView.attributedText = attString;
}
}
七、UITextView放在UITableView上
場景:UITableView上的多個Cell都有UITextView喇嘱,類似于寫文章頁面(當(dāng)然可以采用markdown編寫暇检,這里只是介紹用法)
1、光標(biāo)始終需要顯示在鍵盤上邊
CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.end];
CGPoint point = [textView convertPoint:cursorRect.origin toView:self.tableView];
CGFloat offsetY = point.y - (self.tableView.size.height - keyboardHeight) + cursorRect.size.height + 10;
offsetY = MAX(0, offsetY);
[self.tableView setContentOffset:CGPointMake(0, offsetY) animated:NO];
2婉称、如果鍵盤上需要放工具欄块仆,跟隨鍵盤移動构蹬。當(dāng)textView在響應(yīng)中,刷新列表悔据,同時刷新結(jié)束時textView又需要成為響應(yīng)者庄敛,這時候工具欄會莫名的從上下來。(這種情況不好截圖科汗,當(dāng)你開發(fā)時遇到了知道怎么處理就好了)
// 刷新時關(guān)閉動畫效果即可
[UIView setAnimationsEnabled:NO];
[self.tableView reloadData];
[UIView setAnimationsEnabled:YES];