【问题标题】:create a set of UITextViews that are dynamically sized and laid out in place in relation to each other创建一组动态调整大小并相对于彼此布局的 UITextView
【发布时间】:2014-03-19 05:56:39
【问题描述】:

我有一个自定义 UIView,我想在其中布局一组 UITextView 以响应 AJAX 调用。所以我想我需要在代码中创建这些。我目前将位置设置为固定位置,但宁愿将其设置为比前一个位置低 20 像素。另外,我想将我的 UITextView 的最小宽度设置为 650 像素。我正在使用 AutoLayout,但对实现此目的的最佳方法感到困惑。我已经包含了我的代码,但我真的在寻找最适合实现这一目标的策略?使用自动布局约束?

我已经包含了我当前的操作方式,但想解决动态垂直放置和 UITextView 的固定宽度为 650 点的问题。非常感谢策略或代码方面的任何帮助。

for (int i = 0; i < [some count]; i++) {
    int my_counter=i+1;
    // this is obviously putting it a fixed location
    UITextView *textview= [[UITextView alloc]initWithFrame:CGRectMake(110, (130 * my_counter), 650, 90)];
    [textview setScrollEnabled:YES];
    textview.layer.borderWidth = 5.0f;
    textview.layer.borderColor = [[UIColor grayColor] CGColor];
    [textview  setTextContainerInset:UIEdgeInsetsMake(7, 7, 7, 7)];
    NSString *tmp_header=[some[i] objectForKey:@"header"];
    NSString *tmp_body=[some[i] objectForKey:@"body"];

    //NSString *str = @"This is <font color='red'>simple</font>";
    if(![tmp_header isEqualToString:@""] && ![tmp_body isEqualToString:@""]){
        NSString *myString=[NSString stringWithFormat:@"%@ \n %@", tmp_header, tmp_body];
        [textview setValue:myString forKey:@"contentToHTMLString"];
    }
    if([tmp_header isEqualToString:@""] && ![tmp_body isEqualToString:@""]){
        [textview setValue:@"body is cool" forKey:@"contentToHTMLString"];
    }

    textview.textAlignment = NSTextAlignmentLeft;
    textview.editable = NO;
    textview.font = [UIFont fontWithName:@"vardana" size:20.0];
    [textview sizeToFit];
    [textview setScrollEnabled:NO];
    //textview.contentSize.width=650;
    [self.noteView addSubview:textview];

}

【问题讨论】:

  • 我认为最好的办法是通过代码添加约束..

标签: ios objective-c ios7 uitextview


【解决方案1】:

一般来说,对于这类问题,您需要跟踪循环外的某些内容,以便将先前的结果传递给下一个循环迭代。这段代码应该可以工作:

CGFloat runningYPosition = 130.f; // Y position of first text view
for (int i = 0; i < 10; i++) {
    // Let's not care about the text view frame right now.
    // Let's just create, configure, populate, then set a frame based on contents/configuration.
    UITextView *textview= [[UITextView alloc]initWithFrame:CGRectZero];
    textview.layer.borderWidth = 5.0f;
    textview.layer.borderColor = [[UIColor grayColor] CGColor];
    [textview  setTextContainerInset:UIEdgeInsetsMake(7, 7, 7, 7)];
    NSString *tmp_header=[some[i] objectForKey:@"header"];
    NSString *tmp_body=[some[i] objectForKey:@"body"];

    //NSString *str = @"This is <font color='red'>simple</font>";
    if(![tmp_header isEqualToString:@""] && ![tmp_body isEqualToString:@""]){
        NSString *myString=[NSString stringWithFormat:@"%@ \n %@", tmp_header, tmp_body];
        [textview setValue:myString forKey:@"contentToHTMLString"];
    }
    if([tmp_header isEqualToString:@""] && ![tmp_body isEqualToString:@""]){
        [textview setValue:@"body is cool" forKey:@"contentToHTMLString"];
    }

    textview.textAlignment = NSTextAlignmentLeft;
    textview.editable = NO;
    textview.font = [UIFont fontWithName:@"vardana" size:20.0];

    // Set the text view frame
    CGSize textViewSize = [textview sizeThatFits:CGSizeMake(650.f, CGFLOAT_MAX)];
    textview.frame = CGRectMake(110.f, runningYPosition, textViewSize.width, textViewSize.height);

    [self.view addSubview:textview];

    // Update the running Y position for the next text view's frame, 20 pts below the current one.
    runningYPosition = CGRectGetMaxY(textview.frame) + 20.f;
}

【讨论】:

  • 谢谢 - 这非常有帮助;对此,我真的非常感激。这看起来很简单。
【解决方案2】:

我认为您可以将其作为 UITableView。每个新项目都会被添加到 tableview 的数据数组中,您可以使用 [tableView reloadData] 来显示新项目。

然后你可以使用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

单独调整每个单元格的高度。

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多