ios 使 UITextView 多行

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

Make UITextView multiline

iosiphoneobjective-c

提问by WWJD

I'm building an iOS application and I have a UITextView which I fill with a text. The text view is not big enough to store all the text so it just put "..." so that I can't see the rest of the text I've put in the view. Is there a way for me to make the UITextView to be multiline so that it can store all the text I've put in it?

我正在构建一个 iOS 应用程序,我有一个用文本填充的 UITextView。文本视图不够大,无法存储所有文本,所以它只是放置了“...”,这样我就看不到我在视图中放置的其余文本。有没有办法让 UITextView 成为多行,以便它可以存储我放入的所有文本?

Code :

代码 :

instructionsTextField=[[UITextField alloc] initWithFrame:CGRectZero];
instructionsTextField.frame = CGRectMake(5, 5, self.view.bounds.size.width-10, 90);
instructionsTextField.backgroundColor = [UIColor whiteColor];
instructionsTextField.alpha = 0.5;
instructionsTextField.delegate = self;
instructionsTextField.text = string;
[mapView_ addSubview:instructionsTextField];

SolvedMy problem was that I've said that instructionsTextField is a UITextView but then I've allocated it as UITextField. Now it works fine.

解决了我的问题是我已经说过指令文本字段是一个 UITextView,但后来我将它分配为 UITextField。现在它工作正常。

回答by Atul

You are using UITextFieldhere, it cannot be made multilined(unless you add a view in it). please use UITextViewinstead.

您在这里使用UITextField,它不能成为多行(除非您在其中添加视图)。请改用UITextView

回答by Parvendra Singh

Write it

写下来

     instructionsTextField=[[UITextView alloc] initWithFrame:CGRectZero];
     instructionsTextField.frame = CGRectMake(5, 5, self.view.bounds.size.width-10, 90);
     instructionsTextField.backgroundColor = [UIColor whiteColor];
     instructionsTextField.alpha = 0.5;
     instructionsTextField.delegate = self;
     instructionsTextField.text = string;

     instructionsTextField.scrollEnable=YES;
     instructionsTextField.userInteractionEnabled = YES;
     instructionsTextField.editable=NO;

    [mapView_ addSubview:instructionsTextField];

回答by Buntylm

Make UITextView multiline

The UITextViewclass implements the behavior for a scrollable, multilinetext region. You only need to create the framelike

UITextView类实现了行为的scrollable, multiline文本区域。你只需要创建frame喜欢

UITextView *text = [[UITextView alloc]init];
text.frame = CGRectMake(50, 50, 60, 60);
text.text = @"Your Text. Your Text.Your Text.Your Text.Your Text.Your Text.Your
Text.Your Text.Your Text.Your Text.Your Text.Your Text.Your Text.Your Text.Your 
Text.Your Text.Your Text.Your Text.Your Text.Your Text.Your Text.Your Text.Your";               
[self.view addSubview:text];

this will be automatic scrollable because

这将是自动滚动的,因为

UITextViewInherits from UIScrollView : UIView : UIResponder : NSObject

UITextView继承自 UIScrollView : UIView : UIResponder : NSObject

So there is no need to manage UIScrollonly define your framesize as in the code and add your text will be scroll automatically as per your text.

因此,无需像在代码中那样管理UIScroll只定义您的frame大小,并添加您的文本将根据您的text.

NoteMay be Mistake your side if you talking about UITextFieldthe cannot made multi lined.

注意如果您谈论UITextField无法制作多线,可能会误会您的一面。

回答by Ramdhas

I think you created textview by code,. If yes, increase the textview framesize and contect size. Look at this Question,

我认为您通过代码创建了 textview。如果是,请增加 textview framesize 和 contect size。看看这个问题,

How do I size a UITextView to its content?

如何根据其内容调整 UITextView 的大小?

回答by Stoli

Here is some code to implement a multiline uitextview :

这是一些实现多行 uitextview 的代码:

label = [[UITextView alloc]initWithFrame:CGRectMake(99, 0, widthLabel-14, 112)];
[label setBackgroundColor:[UIColor clearColor]];
label.userInteractionEnabled = YES;
label.delegate = self;
NSString *text = @"Here is my text, replace it with a longer one to see how the uitextview behaves";
NSInteger _stringTotalLength=[text length];
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:text];
[attString addAttribute:NSFontAttributeName
                          value:[UIFont fontWithName:@"Helvetica" size:15.0]
                          range:NSMakeRange(0, _stringTotalLength)];
label.attributedText = attString;

The uitextview will become scrollable when a longer text is inserted.

当插入更长的文本时,uitextview 将变得可滚动。