简体   繁体   English

无法将UITextView调整为其内容的大小

[英]Sizing UITextView to its content isn't working

Here's the code for a UITextView that I want to size to the height of its content. 这是我要确定其内容高度的UITextView的代码。

If I write the textView.frame height explicitly like: 如果我将textView.frame高度显式编写为:

    textView.frame = CGRectMake(100, 12, 320, 458);

the textView sizes to it's content as expected. textView的大小符合预期的内容。

If, however, I write it like the following. 但是,如果我这样写的话。 It doesn't even display although the NSLog statement says that there's a value to textView.contentSize.height 它甚至没有显示虽然NSLog的声明说,有一个价值textView.contentSize.height

UITextView *textView = [[UITextView alloc] init];
textView.layer.borderWidth = 5.0f;

textView.layer.borderColor = [[UIColor redColor] CGColor];

textView.text = [item objectForKey:@"description"];

textView.frame = CGRectMake(100, 12, 320, textView.contentSize.height);


NSLog(@"%f textviewcontnet size", textView.contentSize.height);

textView.editable = NO;

[self.view addSubview:textView];

When I log the output of: 当我记录以下内容的输出时:

NSLog(@"%f textviewcontent size", textView.contentSize.height);

I get "458.000000 textviewcontent size" 我得到“ 458.000000 textviewcontent大小”

thanks for any help 谢谢你的帮助

I'd suggest trying: 我建议尝试:

UITextView *textView = [[UITextView alloc] init];
textView.layer.borderWidth = 5.0f;
textView.layer.borderColor = [[UIColor redColor] CGColor];
textView.text = [item objectForKey:@"description"];
textView.frame = CGRectMake(100, 12, 320, 458);    
textView.editable = NO;

[self.view addSubview:textView];

textView.frame = CGRectMake(100, 12, 320, textView.contentSize.height);    

I've heard that textView.contentSize.height doesn't work until it's been added to a view (though that's not my experience). 我听说textView.contentSize.height在将其添加到视图中之前不起作用(尽管这不是我的经验)。 More importantly, I don't know how it would interpret textView.contentSize.height if it doesn't yet know what the width of the control is. 更重要的是,如果尚不知道控件的宽度是多少,我不知道它将如何解释textView.contentSize.height So go ahead, set the initial frame , do addSubview and then readjust the size based upon textView.contentSize.height . 因此,继续设置初始frame ,执行addSubview ,然后根据textView.contentSize.height重新调整大小。

Quickly copied out of one of my projects: 快速复制出我的项目之一:

AppDelegate *appDelegate;

CGSize  textSize1, textSize2;

appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

textSize1 = [self.subjectLabel.text sizeWithFont:[appDelegate fontNormal] constrainedToSize:CGSizeMake(300.0f, 10000.0f) lineBreakMode:NSLineBreakByWordWrapping];
self.subjectLabel.frame = CGRectMake(10, 5, 300, textSize1.height);
textSize2 = [self.descriptionLabel.text sizeWithFont:[appDelegate fontNormal] constrainedToSize:CGSizeMake(300.0f, 10000.0f) lineBreakMode:NSLineBreakByWordWrapping];
self.descriptionLabel.frame = CGRectMake(10, 5 + textSize1.height + 5, 300, textSize2.height);

[appDelegate fontNormal] just returns a UIFont object, the one that I am using for all "normal" text items. [appDelegate fontNormal]只是返回一个UIFont对象,我正在将其用于所有“常规”文本项。 Don't worry about that too much. 不用太担心。 But it is important that you use the same font that is used for the text view too. 但重要的是您也使用与文本视图相同的字体。

My example is a bit easier because it is a UILable. 我的示例比较简单,因为它是一个UILable。 That works with a text view too but you will have to consider the insects. 这也适用于文本视图,但是您必须考虑昆虫。 Easy solution, just substract some "fuzzy offset" from the width compared to the frame width of your text view. 简单的解决方案,只需减去与文本视图的框架宽度相比的一些“模糊偏移”即可。

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

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