demo
前言:
UITextView的高度隨著輸入文字實(shí)時(shí)的改變是app中非常常見的功能鸳玩,社交軟件的文字輸入框踢京、評論框都會用到
網(wǎng)上有很多UITextView的高度隨著輸入文字實(shí)時(shí)改變的demo赠群,筆者看了很多凹耙,很多雖然可以實(shí)現(xiàn)相應(yīng)的功能但是有些細(xì)節(jié)實(shí)現(xiàn)的不是很好,所以筆者在參考前人的基礎(chǔ)上侠畔,做了些許優(yōu)化贺纲,希望能對讀者有所幫助
一言不合貼代碼:
- 創(chuàng)建UITextView
// 下面這一段代碼毛萌,筆者就不費(fèi)口舌了苟弛,讀者應(yīng)該都看的懂,就是創(chuàng)建一個(gè)外觀類似于UITextField的UITextView
self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake((kMainBoundsWidth-250)/2, kMainBoundsHeight/2-50, 250, 39)];
self.contentTextView .layer.cornerRadius = 4;
self.contentTextView .layer.masksToBounds = YES;
self.contentTextView .delegate = self;
self.contentTextView .layer.borderWidth = 1;
self.contentTextView .font = [UIFont systemFontOfSize:14];
self.contentTextView .layer.borderColor = [[[UIColor lightGrayColor] colorWithAlphaComponent:0.4] CGColor];
//加下面一句話的目的是阁将,是為了調(diào)整光標(biāo)的位置膏秫,讓光標(biāo)出現(xiàn)在UITextView的正中間
self.contentTextView.textContainerInset = UIEdgeInsetsMake(10,0, 0, 0);
[self.view addSubview:self.contentTextView ];
- 實(shí)現(xiàn)的關(guān)鍵:UITextView 的一個(gè)代理方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
這個(gè)代理方法可能很多新手都不太常用做盅,UITextView 每此輸入字符都會走該方法缤削,在這個(gè)方法中實(shí)時(shí)計(jì)算輸入文字的高度,從而動(dòng)態(tài)調(diào)整UITextView 的高度是最合適不過的吹榴。
1.計(jì)算輸入文字高度的方法,之所以返回的高度值加22是因?yàn)閁ITextView有一個(gè)初始的高度值40亭敢,但是輸入第一行文字的時(shí)候文字高度只有18,所以UITextView的高度會發(fā)生變化图筹,效果不太好
- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
CGRect size = [strText boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
context:nil];
float textHeight = size.size.height + 22.0;
return textHeight;
}
2.在- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text帅刀;
中一步步實(shí)現(xiàn)我們的效果:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGRect frame = textView.frame;
float height = [ self heightForTextView:textView WithText:textView.text];
frame.size.height = height;
[UIView animateWithDuration:0.5 animations:^{
textView.frame = frame;
} completion:nil];
return YES;
}
如上面一段代碼,我們在輸入文字的時(shí)候?qū)崟r(shí)計(jì)算textView中的文字远剩,就可以得到我們想要的高度扣溺,效果如何呢?
細(xì)心的讀者可能發(fā)現(xiàn)了瓜晤,在第二行輸入第一個(gè)字的時(shí)候锥余,frame沒有更改,輸入第二個(gè)字的時(shí)候才發(fā)生更改痢掠,第三行同樣如此驱犹,什么原因呢嘲恍?
筆者打斷點(diǎn)調(diào)試后,終于找到問題的根源着绷,回過頭來再看- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
這個(gè)方法,可以看到該方法有兩個(gè)參數(shù)textView 和text锌云,在方法我們計(jì)算的是textView.text的高度荠医,但實(shí)際上這里存在一個(gè)問題,每次輸入文字后調(diào)用該方法桑涎,此時(shí)輸入的文字并不在textView.text中彬向,而在另一個(gè)參數(shù)text中,走完該方法后每次輸入的文字才加入到textView.text中攻冷。筆者選擇的解決方案是將textView.text和text文字拼接起來
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGRect frame = textView.frame;
float height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]];
frame.size.height = height;
[UIView animateWithDuration:0.5 animations:^{
textView.frame = frame;
} completion:nil];
return YES;
}
再來看下效果:
可以看出娃胆,現(xiàn)在滿足了我們剛才的要求,在第二行第一次輸入的時(shí)候等曼,可以更改frame了里烦,但是新的問題又出現(xiàn)了,就是刪除字符的時(shí)候禁谦,當(dāng)?shù)诙斜粍h光的時(shí)候胁黑,沒有直接計(jì)算frame,直到刪除第一行某個(gè)文字的時(shí)候才會計(jì)算州泊,這是為什么呢丧蘸?
筆者調(diào)試過后,發(fā)現(xiàn)按刪除鍵的時(shí)候遥皂,text的文字為
@""
,每次按刪除鍵力喷,調(diào)用該方法的時(shí)候,textView.text此時(shí)并沒有把該字符刪掉演训,所以計(jì)算的還是第一行加第二行文字的高度弟孟,因此我們可以根據(jù)text的內(nèi)容做條件判斷,如果text內(nèi)容為@""
,我們通過截取字符串的方式手動(dòng)去掉一個(gè)字符
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGRect frame = textView.frame;
float height;
if ([text isEqual:@""]) {
height = [ self heightForTextView:textView WithText:[textView.text substringToIndex:[textView.text length] - 1]];
}else{
height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]];
}
frame.size.height = height;
[UIView animateWithDuration:0.5 animations:^{
textView.frame = frame;
} completion:nil];
return YES;
}
效果圖:
基本上實(shí)現(xiàn)了需求样悟,有一個(gè)小bug披蕉,造成了閃退,textView.text為空的時(shí)候乌奇,截取字符串會越界造成閃退没讲,加一個(gè)條件判斷就好了
終極代碼:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGRect frame = textView.frame;
float height;
if ([text isEqual:@""]) {
if (![textView.text isEqualToString:@""]) {
height = [ self heightForTextView:textView WithText:[textView.text substringToIndex:[textView.text length] - 1]];
}else{
height = [ self heightForTextView:textView WithText:textView.text];
}
}else{
height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]];
}
frame.size.height = height;
[UIView animateWithDuration:0.5 animations:^{
textView.frame = frame;
} completion:nil];
return YES;
}
** 結(jié)語:**
筆者只是實(shí)現(xiàn)了簡單的需求,可能很多地方還不夠完善礁苗,也可能存在bug爬凑,歡迎批評,指正试伙,共同交流嘁信。