在ios中动态自动扩展UITextView高度 [英] Autoexpand UITextView height dynamically in ios

查看:175
本文介绍了在ios中动态自动扩展UITextView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的textView动态填充数据。我希望在填充数据后调整textview的高度,以便我看不到垂直滚动,也不会剪切文本。我想以编程方式执行此任务。我有一个标签,应该放置在textview的高度以下20 px,如interest。

I have a simple textView who's data gets populated dynamically. 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. I have a label which should be placed 20 px below height of textview like "interested".

这是我的代码:

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


推荐答案

你可以这样做:

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。

First you have to assign the text before setting its frame for calculating its frame and than you can do sizeToFit. I am sure it'll work. And all below labels or textviews' y position should be relative to that textview.

这篇关于在ios中动态自动扩展UITextView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆