2012-08-31 12 views
1

我可以使用下面的代碼來設置textView框架的大小,以便在按下按鈕或按鈕時大致匹配textView的內容(輸入的文本)。無論何時輸入新字符,我怎麼會這樣調用,以便框架交互地增長或縮小?在我打字時更改UITextView的框架

- (IBAction)doneEditingText:(id)sender { 
    [myTextView resignFirstResponder]; 
    [myTextView setFrame:CGRectMake(myTextView.frame.origin.x, myTextView.frame.origin.y, myTextView.contentSize.width, myTextView.contentSize.height)]; 
} 

感謝您閱讀

回答

4

編輯

在.h文件中實現的UITextView委託這樣的:

@interface ViewController : UIViewController<UITextViewDelegate> 

如果yourTextView從廈門國際銀行則反之在viewDidLoad中與fileowner綁定委託添加此行補充說:

yourTextView.delegate = self; 

使用textView的delega te爲您的要求:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
    CGSize maximumSize = CGSizeMake(280,999); //specify width of textView and maximum height for text to fit in width of textView 
    CGSize txtSize = [textView.text sizeWithFont:[UIFont fontWithName:@"Arial" size:16] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeCharacterWrap]; //calulate size of text by specifying font here 
    //Add UIViewAnimation here if needed 
    [textView setFrame:CGRectMake(textView.frame.origin.x,textView.frame.origin.y,txtSize.width+10,txtSize.height+10)]; // change accordingly 
    return YES; 
} 
+0

這種方法在我輸入時似乎沒有被調用。它應該是? – Mrwolfy

+0

哦,等等,現在是這樣了,因爲我把這一點添加到了viewDidLoad [myTextView setDelegate:self]; – Mrwolfy

+0

查看編輯答案。 –

3

我剛剛完成實現這一點。當前的問題(如發佈這個答案)接受的答案是,委託方法:

- (BOOL)的TextView:(UITextView的*)的TextView shouldChangeTextInRange:(NSRange)範圍replacementText:(的NSString *)文本

在更改用戶輸入/插入/刪除之前暴露textView被提交。因此,你將要實現的調整將是一個人物的遲到。 UITextView不會從UIScrollView繼承,所以文本不會從屏幕上剪下來,但它可能會導致一些尷尬的行爲。

我的解決方案是使用兩個委託方法來正確實現調整大小的效果。

字符之前擴大的UITextView鍵入的用戶點擊屏幕:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
NSMutableString *tempString = [NSMutableString stringWithString:textView.text]; 

[tempString replaceCharactersInRange:range withString:text]; 

//If we are adding to the length of the string (We might need to expand) 
if([tempString length]>textView.text.length) 
{ 
    //Create a temporaryTextView which has all of the characteristics of your original textView 
    UITextView *tempTextView = [[UITextView alloc] initWithFrame:CGRectZero]; 
    tempTextView.font = _inputFont; 
    tempTextView.contentInset = textView.contentInset; 
    [tempTextView setText:tempString]; 

    //Change this to respect whatever width constraint you are trying to achieve. 
    CGSize theSize = [tempTextView sizeThatFits:CGSizeMake(192, CGFLOAT_MAX)]; 

    if(theSize.height!=textView.frame.size.height) 
    { 

     textView.frame = CGRectMake(115, 310, 192,theSize.height); 

     return YES; 
    } 
    else 
    { 
     return YES; 
    } 

} 
else 
{ 
    return YES; 
} 
} 

和收縮用戶已刪除後/縮水在UITextView中的文字量的字符

-(void)textViewDidChange:(UITextView *)textView 
{ 
//We enter this method AFTER the edit has been drawn to the screen, therefore check to see if we should shrink. 
if([textView sizeThatFits:CGSizeMake(192, CGFLOAT_MAX)].height!=textView.frame.size.height) 
{ 
    //change this to reflect the constraints of your UITextView 
    textView.frame = CGRectMake(115, 310, 192,[textView sizeThatFits:CGSizeMake(192, CGFLOAT_MAX)].height); 


} 
} 
相關問題