Ios 创建一组UITEXTVIEW,这些UITEXTVIEW的大小和布局都是动态的,并且相互关联

Ios 创建一组UITEXTVIEW,这些UITEXTVIEW的大小和布局都是动态的,并且相互关联,ios,objective-c,ios7,uitextview,Ios,Objective C,Ios7,Uitextview,我有一个自定义UIView,我想在其中布局一组UITextView以响应AJAX调用。所以我想我需要在代码中创建这些。我目前正在将位置设置为固定位置,但更愿意将其设置为比前一个位置低20像素。另外,我想将UITextView的最小宽度设置为650像素。我正在使用AutoLayout,但对实现这一点的最佳方法感到困惑。我已经包括了我的代码,但我真的在寻找实现这一点的最佳策略?使用自动布局约束 我已经介绍了我目前是如何做的,但我想解决动态垂直放置问题,并将UITextView的固定宽度设置为650点

我有一个自定义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];

}
for(int i=0;i<[一些计数];i++){
int my_counter=i+1;
//这显然是把它放在一个固定的位置
UITextView*textview=[[UITextView alloc]initWithFrame:CGRectMake(110,(130*my_计数器),650,90)];
[textview SetScrolEnabled:是];
textview.layer.borderWidth=5.0f;
textview.layer.borderColor=[[UIColor grayColor]CGColor];
[textview settextContainerSet:UIEdgeInsetsMake(7,7,7,7)];
NSString*tmp_header=[some[i]objectForKey:@“header”];
NSString*tmp_body=[some[i]objectForKey:@“body”];
//NSString*str=@“这很简单”;
如果(![tmp_头IsequalString:@”“]&&![tmp_体IsequalString:@”“){
NSString*myString=[NSString stringWithFormat:@“%@\n%@”,tmp_头,tmp_体];
[textview设置值:myString forKey:@“contentToHTMLString”];
}
if([tmp_头IsequalString:@”“]&&![tmp_体IsequalString:@”“){
[textview设置值:@“body is cool”forKey:@“contentToHTMLString”];
}
textview.textAlignment=NSTextAlignmentLeft;
textview.editable=否;
textview.font=[UIFont fontWithName:@“vardana”大小:20.0];
[textview sizeToFit];
[文本视图设置可克隆:否];
//textview.contentSize.width=650;
[self.noteView addSubview:textview];
}

通常,对于此类问题,您需要跟踪循环之外的内容,以便将以前的结果传递给下一个循环迭代。此代码应适用于:

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;
}
CGFloat runningYPosition=130.f;//第一个文本视图的Y位置
对于(int i=0;i<10;i++){
//现在我们不必关心文本视图框架。
//让我们创建、配置、填充,然后根据内容/配置设置一个框架。
UITextView*textview=[[UITextView alloc]initWithFrame:CGRectZero];
textview.layer.borderWidth=5.0f;
textview.layer.borderColor=[[UIColor grayColor]CGColor];
[textview settextContainerSet:UIEdgeInsetsMake(7,7,7,7)];
NSString*tmp_header=[some[i]objectForKey:@“header”];
NSString*tmp_body=[some[i]objectForKey:@“body”];
//NSString*str=@“这很简单”;
如果(![tmp_头IsequalString:@”“]&&![tmp_体IsequalString:@”“){
NSString*myString=[NSString stringWithFormat:@“%@\n%@”,tmp_头,tmp_体];
[textview设置值:myString forKey:@“contentToHTMLString”];
}
if([tmp_头IsequalString:@”“]&&![tmp_体IsequalString:@”“){
[textview设置值:@“body is cool”forKey:@“contentToHTMLString”];
}
textview.textAlignment=NSTextAlignmentLeft;
textview.editable=否;
textview.font=[UIFont fontWithName:@“vardana”大小:20.0];
//设置文本视图框架
CGSize textViewSize=[textview sizeThatFits:CGSizeMake(650.f,CGFLOAT_MAX)];
textview.frame=CGRectMake(110.f,runningYPosition,textViewSize.width,textViewSize.height);
[self.view addSubview:textview];
//更新下一个文本视图帧的运行Y位置,比当前帧低20分。
runningYPosition=CGRectGetMaxY(textview.frame)+20.f;
}

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

然后你可以使用

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

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

我认为最好是通过代码添加约束。..thx-这非常有用;我真的很感激。这看起来很简单。