我有一个简单的textView动态填充数据。我想在填充数据后调整textview的高度,以便我看不到垂直滚动,也不会剪切文本。我想以编程方式执行此任务。我有一个标签应该放在文本视图高度以下20 px,如“interest”。
这是我的代码:
lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(10, 310, 300, 20)];
lblHobbies.text=@"Hobbies";
lblHobbies.font=[UIFont systemFontOfSize:16.0];
lblHobbies.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[scrollView addSubview:lblHobbies];
tViewhobbies=[[UITextView alloc]initWithFrame:CGRectMake(10, 330, 300, 60)];
tViewhobbies.backgroundColor=[UIColor clearColor];
tViewhobbies.layer.cornerRadius=5;
[tViewhobbies setFont:[UIFont systemFontOfSize:13.0]];
[tViewhobbies setUserInteractionEnabled:NO];
tViewhobbies.font=[UIFont systemFontOfSize:16.0];
tViewhobbies.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
tViewhobbies.delegate=self;
tViewhobbies.scrollEnabled=YES;
[scrollView addSubview:tViewhobbies];
lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(10, 370, 300, 20)];
lblInterests.text=@"Interests";
lblInterests.font=[UIFont systemFontOfSize:16.0];
lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[scrollView addSubview:lblInterests];
tViewInterests=[[UITextView alloc]initWithFrame:CGRectMake(10, 390, 300, 60)];
tViewInterests.backgroundColor=[UIColor clearColor];
[tViewInterests setFont:[UIFont systemFontOfSize:13.0]];
[tViewInterests setUserInteractionEnabled:NO];
tViewInterests.font=[UIFont systemFontOfSize:16.0];
tViewInterests.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
tViewInterests.delegate=self;
tViewInterests.scrollEnabled=YES;
[scrollView addSubview:tViewInterests];
答案 0 :(得分:0)
您可以通过UITextview的sizeToFit方法尝试。
[myTextView sizeToFit];
答案 1 :(得分:0)
这取决于字体大小。下面的代码检索文本的字体大小并返回所需的高度和宽度。这可以帮助您创建逻辑:
NSDictionary *attribs1 = @{
NSFontAttributeName: tViewhobbies.font
};
CGSize textViewSize = [tViewhobbies.text sizeWithAttributes:attribs1];
答案 2 :(得分:0)
以下是我一直使用的技巧......
UITextView
完成....
答案 3 :(得分:0)
您可以执行以下操作:
tViewhobbies=[[UITextView alloc]init];
tViewhobbies.backgroundColor=[UIColor clearColor];
[tViewhobbies setText:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."];
CGSize maxSize = CGSizeMake(300.0f, CGFLOAT_MAX);
CGSize requiredSize = [tViewhobbies sizeThatFits:maxSize];
tViewhobbies.frame = CGRectMake(10,330, requiredSize.width, requiredSize.height);
tViewhobbies.layer.cornerRadius=5;
[tViewhobbies setUserInteractionEnabled:NO];
tViewhobbies.font=[UIFont systemFontOfSize:16.0];
tViewhobbies.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
tViewhobbies.delegate=self;
tViewhobbies.scrollEnabled=NO;
[scrollView addSubview:tViewhobbies];
[tViewhobbies sizeToFit];
首先,您必须在设置其框架之前分配文本以计算其框架,而不是您可以sizeToFit
。我相信它会奏效。并且所有下方标签或文本视图的y位置应该相对于该文本视图。