2011-09-30 87 views

回答

1

我上面code.try已經更新了它。我有覺得你有慾望的結果..

// Create a size with large height, but matching width to UITextView. 
[myTextView setBackgroundColor:[UIColor greenColor]]; 
CGSize maxSize = CGSizeMake(myTextView.frame.size.width, 20); 

// Determine the size needed to display the text - we just need the new height. 
CGSize textSize = [myTextView.text sizeWithFont:myTextView.font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; 

// Set the UITextView's frame to the new height. 
CGRect textViewRect = myTextView.frame; 
textViewRect.size.height = textSize.height+13; 
myTextView.frame = textViewRect; 
2

是這樣的嗎?

textView.text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

0

我猜想/猜測你想調整一個UITextView很好地容納文本。如果是的話,我建議你的調查:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode; 

例如:

// Create a size with large height, but matching width to UITextView. 
CGSize maxSize = CGSizeMake(myTextView.frame.size.width, 9999); 

// Determine the size needed to display the text - we just need the new height. 
CGSize textSize = [myTextView.text sizeWithFont:myTextView.font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; 

// Set the UITextView's frame to the new height. 
CGRect textViewRect = myTextView.frame; 
textViewRect.size.height = textSize.height; 
myTextView.frame = textViewRect; 
+0

我必須寫 - (CGSize)sizeWithFont:(UIFont *)字體constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode; .h文件中的 方法? – PJR

+0

textViewRect.height = textSize.height; 行顯示錯誤 – PJR

+0

不,這是一個NSString實例方法,因此您可以將它包含在代碼中,而不必在界面中聲明它。 –