【问题标题】:UIScrollView and UITextView autosizing possible?UIScrollView 和 UITextView 可以自动调整大小吗?
【发布时间】:2011-11-06 11:19:31
【问题描述】:

我有一个通过导航控制器推送到屏幕上的视图:

里面是一个 UIScrollView。

然后在 UIScrollView 内部是一些静态对象,例如图像和标签。

然后是难点,有一个 UITextView,其文本从不同长度的不同文本文件中加载。

我需要能够将 UITextView 的大小动态调整到其内容,对于 UIScrollView 也是如此。这可能吗?

【问题讨论】:

    标签: iphone objective-c ios xcode xcode4


    【解决方案1】:

    是的,你可以这样做:

    if ([textView length] > int//any number you want) {
        textView.frame = CGRectMake(//just adjust the size  an position here);
        scrollView.contentSize = CGSizeMake(//adjust scrollView size)
    }
    else if ([textView length] > int//just another number) {
        // you can continue looping that for how often you want
    }
    

    对于 if 语句中的 int,您检查文本的长度。基于此,您可以调整 scrollView 和 textView 的大小。

    【讨论】:

      【解决方案2】:

      您可以在以下代码的帮助下做到这一点。我已经为 Label 完成了该代码,并且您可以在 text-field 的帮助下执行此操作。

      NSString *cellText = "Text Of Your Text-Field";
      UIFont *cellFont = [UIFont fontWithName:@"Your Font Name" size:FONT_SIZE];//UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:13.0];
      CGSize constraintSize = CGSizeMake(@"Width Of Your Text-Field", MAXFLOAT);//CGSize constraintSize = CGSizeMake(220.0f, MAXFLOAT);
      CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
      
      int height = labelSize.height;
      
      frame.origin.x = Starting Position of X;
      frame.origin.y = Starting Position of Y;
      frame.size.width = Width Of Your TextField;
      frame.size.height = height;
      
      UILabel *lblName = [[[UILabel alloc] initWithFrame:frame] autorelease];
      lblName.numberOfLines = 0;
      lblName.lineBreakMode = UILineBreakModeWordWrap;
      lblName.textAlignment = UITextAlignmentLeft;
      lblName.textColor = [UIColor whiteColor];
      lblName.backgroundColor = [UIColor clearColor];
      lblName.font = [UIFont boldSystemFontOfSize:13.0];
      

      对于滚动视图,您也可以采用同样的方式。只需设置该滚动视图的 Frame 即可。

      【讨论】:

        【解决方案3】:
        float length = [yourText length];
        textview.frame = CGRectMake(44, 87, 923, ceilf(length/142)*25);
        

        这里的25 是假定为文本字体宽度的常量值。从这里你可以设置滚动视图框架对文本视图框架的引用。

        【讨论】:

          猜你喜欢
          • 2019-06-25
          • 1970-01-01
          • 2012-11-24
          • 1970-01-01
          • 2020-07-20
          • 2011-03-03
          • 2014-01-18
          • 1970-01-01
          相关资源
          最近更新 更多