如何根据UITextView的内容大小动态更改UIView的Y轴

时间:2012-07-25 12:27:22

标签: iphone ios uiview height uitextview

我有一个UITextView作为UIView的子视图。我把UIView放在了屏幕的底部。视图控制器具有UITabViewController。我把框架尺寸给了UIView,如下所示,

toolBarView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 330, 320, 40)];
[self.view addSubview: toolBarView1];
TextView1 = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
textViewImageView1 = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 25)];
[TextView1 addSubview: textViewImageView1];
[toolBarView1 addSubview: TextView1];

我想根据UITextView中的文本增加UITextView高度。我使用了以下代码,

    CGRect frame = messageTextView1.frame;
    frame.size.height = messageTextView1.contentSize.height;    

    NSLog(@"TextViewHeight With Texts : %f", frame.size.height);
    int height = frame.size.height;
    NSLog(@"TextViewHeight With Texts : %d", height);

    height = height + 10;
    NSLog(@"Height Plus 10 : %d", height);
    height = 370 - height;
    NSLog(@"ToolBarView1 Y axis : %d", height);


    messageTextView1.frame = frame;
    textViewImageView1.frame = CGRectMake(0, 0, messageTextView1.frame.size.width, messageTextView1.frame.size.height);
    toolBarView1.frame = CGRectMake(0, height, 320, messageTextView1.frame.size.height+10);

我想将UIView和UITextView置于屏幕底部,但我想根据UITextview高度增加UIView的UIView和UITextview以及Y轴的高度。有人可以帮我这样做吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

你可以像这样预览uitextview高度

    CGFloat constrainedSize = 252.0f;//your uitextview widght
    UIFont * myFont = [UIFont fontWithName:@"Arial" size:19];//your uitextview font
    CGSize textSize = [dis.text sizeWithFont: myFont
                          constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
                              lineBreakMode:UILineBreakModeWordWrap];
    CGRect textViewFrame = CGRectMake (0, 0, textSize.width, textSize.height);

然后你可以从textViewFrame

获得你的uitextview高度