ios UITextView 自动高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7237449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:18:00  来源:igfitidea点击:

UITextView Auto Height

iphoneobjective-ciosxcodeipad

提问by Rollo

How do I make a UITextView expand or shrink to the size of the text inside it and then make the UIScrollview Expand or shrink to fit the UITextView

如何使 UITextView 扩展或收缩到其中的文本大小,然后使 UIScrollview 扩展或收缩以适合 UITextView

回答by bryanmac

CGRect frame;
frame = textiew.frame;
frame.size.height = [textView contentSize].height;
textView.frame = frame;

One thing to note is that the correct contentSize is only available afterthe UITextView has been added to the view with addSubview and the text is assigned to it. Prior to that it is equal to frame.size

需要注意的一件事是,正确的 contentSize 仅使用 addSubview 将 UITextView 添加到视图并为其分配文本后才可用。在此之前它等于 frame.size