ios 使用文本以编程方式更改 UITextView 高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11254301/
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 19:02:25  来源:igfitidea点击:

change UITextView height programmatically with the text

iphoneiosuitextviewcgrect

提问by Piero

i want change the UITextView programmatically with the amount of text i set, and i have a problem, if i add a the UITextView with interface builder and i do this:

我想用我设置的文本量以编程方式更改 UITextView,并且我有一个问题,如果我添加带有界面构建器的 UITextView 并且我这样做:

CGRect frame = textViewA1.frame;
frame.size.height = textViewA1.contentSize.height;
textViewA1.frame = frame;

all work fine, but if i create the UITextView programmatically, the height don't change, i do this:

一切正常,但如果我以编程方式创建 UITextView,高度不会改变,我这样做:

 UITextView *textViewA1 = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 320, 50)];
[textViewA1 setFont:[UIFont fontWithName:@"Enriqueta" size:15]];
[textViewA1 setScrollEnabled:NO];
[textViewA1 setUserInteractionEnabled:NO];
[textViewA1 setBackgroundColor:[UIColor clearColor]];
[textViewA1 setText:@"A lot of text"];
CGRect frame = textViewA1.frame;
frame.size.height = textViewA1.contentSize.height;
textViewA1.frame = frame;

in this way the height size of the uitextview don't change, how i can do?

这样 uitextview 的高度大小不会改变,我该怎么办?

回答by rob mayoff

Just send the sizeToFitmessage to the UITextView. It will adjust its own height to just fit its text. It will not change its own width or origin.

只需将sizeToFit消息发送到UITextView. 它将调整自己的高度以适合其文本。它不会改变自己的宽度或原点。

[textViewA1 sizeToFit];

回答by zolibra

i think you need to do :

我认为你需要做:

CGRect frame = textViewA1.frame;
frame.size.height = textViewA1.contentSize.height;
textViewA1.frame = frame;

after the addsubview:

在 addsubview 之后:

[self.view addSubview: self.textView];

回答by davidgoli

You need to calculate the frame size for the text you have, for instance:

您需要计算您拥有的文本的框架大小,例如:

UIFont *font = [UIFont fontWithName:@"Enriqueta" size:15];
NSString *text = @"A lot of text";
CGSize frameSize = [text sizeWithFont:font];
CGRect originalFrame = textViewA1.frame;
textViewA1.frame = CGRectMake(CGRectGetMinX(originalFrame), CGRectGetMinY(originalFrame), frameSize.width, frameSize.height);

To keep it from exceeding a particular height, use:

要防止它超过特定高度,请使用:

CGFloat maxHeight = 100; // or something
CGSize frameSize = [text sizeWithFont:font constrainedToSize:maxHeight];

回答by trumpetlicks

Try this code

试试这个代码

UIFont * font = [UIFont fontWithName:@"Enriqueta" size:15];
textViewA1.font = font;
NSString * theText = @"A lot of text";
CGSize theStringSize = [theText sizeWithFont:font constrainedToSize:CGSizeMake(190, 1000000) lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = textViewA1.frame;
frame.size.height = theStringSize.height;
textViewA1.frame = frame;

This of course has a static width of 190 pixels for the textview. You can obviously change that!!!

这当然具有 190 像素的文本视图静态宽度。你显然可以改变它!!!

回答by codercat

to scroll UITextViewwhen added multiple line in textview

UITextView在 textview 中添加多行时滚动

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    CGRect frameTextView = textView.frame;
    frameTextView.height -= KEY_BOARD_HEIGHT;
    textView.frame = frameTextView;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
{
    CGRect frameTextView = textView.frame;
    frameTextView.height += KEY_BOARD_HEIGHT;
    textView.frame = frameTextView;
}