在textViewDidChange
,
CGSize constraintSize;
CGSize labelSize;
constraintSize = CGSizeMake(myTextView.frame.size.width, 480);
NSString *text = myTextView.text;
text = [text stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
text = [text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
if([text stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding])
{
text = [text stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}
// Note that in the below line you must use the font as exact as you use for textview
labelSize = [text sizeWithFont:[UIFont systemFontOfSize: 16] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
在中labelSize
您可以找到文本视图的预期大小。您可以将框架设置myTextView
为
myTextView.frame = CGRectMake(myTextView.frame.origin.x,myTextView.frame.origin.y,labelSize.width,labelSize.height);
认为这对你有用。试试看。希望这可以帮助。