简体   繁体   English

如何根据大小调整UITextView的宽度?

[英]How to resize the width of the UITextView according the?

I want to ask how to resize the UITextView to fit the text will display on the screen. 我想问一下如何调整UITextView大小以适合将显示在屏幕上的文本。

For example 例如

在此处输入图片说明

Resize the UITextView to UITextView 调整

在此处输入图片说明

Could anyone help me out or give me some advise. 谁能帮助我或给我一些建议。 Thanks 谢谢

Tips: 提示:

I tried sizeWithFont:constrainedToSize: ,Not work 我尝试过sizeWithFont:constrainedToSize:不起作用

If you need to know the CGSize that is taken by your NSString if rendered on a single line, use CGSize sz = [textView.text sizeWithFont:_textView.font] then adjust your frame given this size value. 如果您需要了解CGSize是采取您NSString如果在一行渲染,使用CGSize sz = [textView.text sizeWithFont:_textView.font]再给予这个大小值调整框架。

If you need to know the size taken by your text if rendered on multiple lines, wrapping it if the text reaches a given width, use sizeWithFont:constrainedToSize:lineBreakMode: instead, etc. 如果您需要知道多行呈现时文本的size ,如果文本达到给定的宽度则将其包装,请使用sizeWithFont:constrainedToSize:lineBreakMode:等。

I had the same problem some time ago (but with the height of the UITextView ) and I created a CGRect with a size that would fit the contentSize of the UITextView , Try the code below 前段时间我遇到了同样的问题(但是具有UITextView高度 ),并且我创建了一个CGRect ,其大小适合UITextViewcontentSize ,请尝试下面的代码

// assuming that you have a UITextView named textView with some text on it
CGRect textFrame = self.textView.frame;
textFrame.size.width = self.textView.contentSize.width;
self.textView.frame = textFrame;

try this one it worked for me 试试这个对我有用的

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
 frame.size.width = _textView.contentSize.width;
_textView.frame = frame;

happy coding.... 快乐的编码。

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

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