【问题标题】:How to change the UIView's Y axis dynamically based on UITextView's Content size如何根据 UITextView 的内容大小动态改变 UIView 的 Y 轴
【发布时间】:2012-07-23 21:10:57
【问题描述】:

我有一个 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 和 UITextview 的高度以及 UIView 的 Y 轴。谁能帮我做到这一点?提前致谢。

【问题讨论】:

    标签: iphone ios uiview height uitextview


    【解决方案1】:

    你可以像这样预览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 高度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 2014-03-30
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多