简体   繁体   English

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

[英]Autoexpand UITextView height dynamically in ios

I have a simple textView who's data gets populated dynamically. 我有一个简单的textView,谁的数据动态填充。 I want to resize the height of the textview once the data is populated so that I don't see a vertical scroll nor the text gets clipped.i want to do this task programatically. 我想在填充数据后重新调整textview的高度,以使我看不到垂直滚动或文本被剪切。我想以编程方式执行此任务。 I have a label which should be placed 20 px below height of textview like "interested". 我有一个标签,应该放置在textview高度以下20像素以下,例如“有兴趣”。

This is my code: 这是我的代码:

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

This depends on font size. 这取决于字体大小。 Following code retrieve the font size of text and return the height and width required. 以下代码检索文本的字体大小,并返回所需的高度和宽度。 This may help you to create the logic: 这可以帮助您创建逻辑:

NSDictionary *attribs1 = @{
                          NSFontAttributeName: tViewhobbies.font
                          };

CGSize textViewSize = [tViewhobbies.text sizeWithAttributes:attribs1];

Below is the trick I always use... 以下是我一直使用的技巧...

  1. Create one fake UITextView 创建一个伪造的UITextView
  2. Put data into it 将数据放入其中
  3. Call sizeToFit 通话大小调整
  4. Hide this textview 隐藏此文字检视
  5. Set actual textview height based on this fake uitextview... 根据这个假的uitextview设置实际的textview高度...

Done.... 完成...

You can do something like: 您可以执行以下操作:

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

First you have to assign the text before setting its frame for calculating its frame and than you can do sizeToFit . 首先,必须先设置文本,然后再设置其框架以计算其框架,然后可以执行sizeToFit I am sure it'll work. 我相信它会起作用。 And all below labels or textviews' y position should be relative to that textview. 并且所有下面标签或文本视图的y位置都应相对于该文本视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM