如何在iPhone中增加UITextView高度和X轴值?

时间:2012-07-05 13:29:18

标签: iphone ios uiview uitextview

使用UITextView在iPhone应用中工作。我试图根据它的内容长度增加UITextView的高度。它工作正常。但是,当uitextview的高度增加时,UITextView应该改变上方的高度而不是增加高度。

这是我在项目中尝试的代码,

    baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 50)];
    baseView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:baseView];

    TextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
    TextView.backgroundColor = [UIColor whiteColor];
    TextView.layer.borderWidth = 2;
    TextView.delegate = self;
    TextView.font = [UIFont fontWithName:@"Helvetica" size:16];
    [baseView addSubview: TextView];

    -(void)textViewDidChange:(UITextView *)textView
   {
       CGRect frame = messageTextView.frame;
       frame.size.height = messageTextView.contentSize.height;
       TextView.frame = CGRectMake(20, 10, 280, frame.size.height);
       baseView.frame = CGRectMake(0, 50, 320, frame.size.height+20);
   }

当UITextView高度改变时,效果textview高度将在顶部增加。有人可以帮我这个吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

在方法中更改框架,同时修改frame.origin.y

textView
-(void)textViewDidChange:(UITextView *)textView
{
   CGRect frame = messageTextView.frame;
   frame.size.height = messageTextView.contentSize.height;
   TextView.frame = CGRectMake(20, 0, 280, frame.size.height); //previous y was 10
   baseView.frame = CGRectMake(0, 50, 320, frame.size.height+20);
}

尽管如此,我相信您知道textView上只有10个像素,因为您要将其添加到BaseView视图中。