在ios中动态自动扩展UITextView高度

我有一个简单的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]; 

您可以通过UITextview的sizeToFit方法尝试它。

 [myTextView sizeToFit]; 

这取决于字体大小。 以下代码检索文本的字体大小并返回所需的高度和宽度。 这可以帮助您创建逻辑:

 NSDictionary *attribs1 = @{ NSFontAttributeName: tViewhobbies.font }; CGSize textViewSize = [tViewhobbies.text sizeWithAttributes:attribs1]; 

以下是我一直使用的技巧……

  1. 创建一个假的UITextView
  2. 将数据放入其中
  3. 调用sizeToFit
  4. 隐藏此文本视图
  5. 根据这个虚假的uitextview设置实际的textview高度…

完成….

你可以这样做:

 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 。 我相信它会奏效。 并且所有下面的标签或textviews的y位置应该相对于该textview。