2013-10-04 44 views
0

1)我想在TextView.When TextView高度增加時啓用ScrollView。我想在TextView中啓用ScrollView.When TextView高度增加

2)當我按下輸入完整行後輸入我正在輸入的內容顯示。但是當我按下輸入時只有2或3個字母輸入我正在輸入的內容沒有顯示。按回車後顯示

-(void)viewDidLoad 
{ 
    UITextView *txtView=[[UITextView alloc]init]; 
    txtView.frame = CGRectMake(60, 100, 350, 154); 
    txtView.backgroundColor=[UIColor whiteColor]; 
    txtView.font=[UIFont fontWithName:Nil size:30];/*Font size*/ 
    txtView.delegate=self; 
    txtView.scrollEnabled=YES; 
    [self.view addSubview:txtView]; 
} 
-(void)textViewDidChange:(UITextView *)textView 
{ 
if(textView.contentSize.height>textView.frame.size.height) 
{ 
CGRect frame = txtView.frame; 
frame.size.height = textView.contentSize.height+90; 
textView.frame = frame; 
textView.scrollEnabled=YES; 
} 
textView.scrollEnabled=YES; 
} 
+0

我不明白,爲什麼* *您想手動更改的TextView的contentsize。 UITextView負責所有的事情,包括內容和滾動。除非您想將contentize設置爲超過實際內容的高度,否則不應設置contentize或scrollEnabled。 – n00bProgrammer

+0

另外,你所做的是改變textView框架的高度。如果你想有一個TextView,根據它的內容改變高度,看看[HPGrowingTextView](https://github.com/yatinsns/HPGrowingTextView) – n00bProgrammer

+0

txtView = [[UITextView alloc] init]; txtView.frame = CGRectMake(60,100,350,154); txtView.backgroundColor = [UIColor whiteColor]; txtView.font = [UIFont fontWithName:Nil size:30]; – user2677311

回答

0
if(textView.contentSize.height > textView.frame.size.height) 
{ 
    CGRect frame = textView.frame; 
    frame.size.height = textView.contentSize.height + 90; 
    textView.frame = frame; 
    textView.scrollEnabled = YES; 

    [textView setContentSize:CGSizeMake(0, textView.frame.size.height + 1)]; 
} 
+0

如果我像這樣嘗試scrollView禁用textView中,我不想增加textHeight.Because下面的textView我有一個提交按鈕。 – user2677311

+0

請清楚說明你想從textView中得到什麼樣的行爲。 是否要更改textView的框架高度,還是隻啓用滾動? – n00bProgrammer

+0

我需要1件東西。1)當用戶輸入文本時應該顯示文本。例如,textView高度是4行。當用戶到達第4行的末尾時,它會自動進入下一行,它顯示了什麼正在打字。在我的情況下,輸入第四行字母后,每行只輸入一個字母。我按下回車鍵,然後光標移到下一行,我輸入的內容不會顯示。按文字 – user2677311