gpt4 book ai didi

ios - 创建一组 UITextViews,这些 UITextViews 可以动态调整大小并相对于彼此布置到位

转载 作者:行者123 更新时间:2023-11-29 03:14:07 26 4
gpt4 key购买 nike

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

}

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

最佳答案

通常,对于此类问题,您需要跟踪循环外的某些内容,以将先前的结果传递给下一个循环迭代。此代码应该有效:

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;
}

关于ios - 创建一组 UITextViews,这些 UITextViews 可以动态调整大小并相对于彼此布置到位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com