7

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?

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];

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

Andro
  • 2,232
  • 1
  • 27
  • 40
WWJD
  • 1,104
  • 4
  • 12
  • 31

5 Answers5

33

You are using UITextField here, it cannot be made multilined(unless you add a view in it). please use UITextView instead.

Atul
  • 698
  • 4
  • 13
3
Make UITextView multiline

The UITextView class implements the behavior for a scrollable, multiline text region. You only need to create the frame like

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

UITextView Inherits from UIScrollView : UIView : UIResponder : NSObject

So there is no need to manage UIScroll only define your frame size as in the code and add your text will be scroll automatically as per your text.

Note May be Mistake your side if you talking about UITextField the cannot made multi lined.

Buntylm
  • 7,345
  • 1
  • 31
  • 51
3

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];
Parvendra Singh
  • 965
  • 7
  • 19
1

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

How do I size a UITextView to its content?

Community
  • 1
  • 1
Ramdhas
  • 1,765
  • 1
  • 18
  • 26
1

Here is some code to implement a multiline 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.

Wolfram
  • 8,044
  • 3
  • 45
  • 66
Stoli
  • 69
  • 2
  • 14