在文本累积时使textView水平缩放

时间:2012-08-31 12:09:01

标签: iphone ios xcode uitextview

我试图在文本累积时水平制作textView框架比例。我正在使用代码打击试图实现这一点。框架按预期垂直缩放,但大小似乎水平锁定。

 [myTextView setFrame:CGRectMake(myTextView.frame.origin.x, myTextView.frame.origin.y, myTextView.contentSize.width, myTextView.contentSize.height)];

Here is the project(27K)如果有人想看看。

感谢阅读。

2 个答案:

答案 0 :(得分:1)

您的回答位于notes-app

答案 1 :(得分:1)

在.h文件中实现UITextView委托:

@interface ViewController : UIViewController<UITextViewDelegate>

如果从xib添加了yourTextView,则将delegate与fileowner绑定,否则在ViewDidLoad中添加以下行:

yourTextView.delegate = self;

根据您的要求使用textView的委托:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
   CGSize maximumSize = CGSizeMake(2000,40); //specify height of textView  and maximum width for text to fit in height of textView as u want text horizontally
   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;
}