-1

I am downloading UITextView contents from a web server and it just returns text without any line feeds. I inserted \n, \\n, [CR], and more into the place I want to change the line but none of these worked. How can I change the line for the text in UITextView?

jscs
  • 63,694
  • 13
  • 151
  • 195
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
  • Show us the code you are using to perform this insertion. – jscs Mar 21 '11 at 23:03
  • I manually added into the original text.. – codereviewanskquestions Mar 21 '11 at 23:29
  • That doesn't tell me anything. When I create a `UITextView` and set its text to a string that has line feeds, the text is displayed in separate lines. There's no way for anyone to tell what your trouble is unless you post your code. – jscs Mar 22 '11 at 01:16

2 Answers2

0

Weird. Works for me.

_subtypeField = [[UITextView alloc] initWithFrame:CGRectMake(28, 189, self.bounds.size.width-30, 100)];
[_subtypeField setFont:[UIFont fontWithName:@"Helvetica" size:13]];
[_subtypeField setText:@"Examples:\n- Fentanyl\n- Dilaudid 2 mg PO q 4 hours prn moderate pain"];
[_subtypeField setTextAlignment:UITextAlignmentLeft];
[_subtypeField setTextColor:[UIColor colorWithRed:0.494 green:0.647 blue:0.808 alpha:1.0]];
[_subtypeField setOpaque:NO];
[_subtypeField setBackgroundColor:[UIColor clearColor]];
[_subtypeField setUserInteractionEnabled:NO];
[self addSubview:_subtypeField];

Perhaps something goes wrong when you convert the C string into an NSString? Are the linefeeds still in the NSString?

Vagrant
  • 1,696
  • 12
  • 19
0

If your text that you want to display is dynamic and the width of text view is fixed then you can first calculate the height of textview according to the text and then display that text in text view. This will automatically change the line in the text view

Code that I used:-

    CGSize labelsize;
    UITextView *commentsTextView = [[UITextView alloc] init];;
    [commentsTextView setBackgroundColor:[UIColor clearColor]];
    NSString *text=@"sample Text";//give your text
    [commentsTextView setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize=[text sizeWithFont:commentsTextView.font constrainedToSize:CGSizeMake(268, 2000.0)];
    commentsTextView.frame=CGRectMake(10, 24, 268, labelsize.height);
    [self.view addSubview:commentsTextView];
    [commentsTextView release];
Gypsa
  • 11,230
  • 6
  • 44
  • 82