创建一组动态调整大小的UITextView,并相互放置

时间:2014-02-19 04:56:10

标签: ios objective-c ios7 uitextview

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

我已经包含了我目前正在做的事情,但是想要解决动态verticasl位置和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];

}

here's a sample layout of the objects - very straightforward

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

我认为你可以把它做成UITableView。每个新项目都将添加到tableview的数据数组中,您可以使用[tableView reloadData]显示新项目。

然后你可以使用

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

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