0

I have a large text file that I am trying to display on screen. For example:

Line 1
Line 2
Line 3
...
Line 138

I have a UIScrollview, and dynamically add a subview of either a UILabel or UITextView with the texts. I am able to calculate the width/height of the text with this:

[passedTextInFile boundingRectWithSize:CGSizeMake(20000.0f, 20000.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Times New Roman" size:textSize]} context:Nil].size;

Then I create my UILabel and add to subview like this:

CGRect test = CGRectMake(5.0, 5.0, maxWidthSize, maxHeightSize);
CGSize maxSize = CGSizeMake(maxWidthSize, maxHeightSize);
UILabel *lblResults = [[UILabel alloc] initWithFrame:test];
[lblResults setFrame:test];
[lblResults setText:passedTextInFile];
[lblResults setFont:[UIFont fontWithName:@"Times New Roman" size:textSize]];
[lblResults setTextColor:[UIColor blackColor]];
[lblResults setNumberOfLines:0];

[svResults addSubview:lblResults];
[svResults setShowsHorizontalScrollIndicator:true];
[svResults setScrollEnabled:true];
[svResults setContentSize:maxSize];

The above code works great. I am able to see Line 1, and scroll to the bottom and see Line 138.

However, when I change the UILabel to UITextView, I am only able to scroll to Line 131.

UITextView *lblResults = [[UITextView alloc] initWithFrame:test];
[lblResults setFrame:test];
[lblResults setText:passedTextInFile];
[lblResults setFont:[UIFont fontWithName:@"Times New Roman" size:textSize]];
[lblResults setTextColor:[UIColor blackColor]];
[lblResults setScrollEnabled:false];
[lblResults setEditable:false];

It seems to cut off some of the text at the end. I have double checked that the Height Size is both 2139.539062 for UILabel and UITextView.

Now, when I enable scroll for the UITextView, I am able to scroll to Line 131, let the scrolling stop, then can scroll to Line 138. In other words, I am scrolling to Line 131 in the UIScrollView, and then to Line 138 in the UITextView. I do no want this behavior, which is why I disabled scrolling for the UITextView.

My question is why does the scrolling stop early in a UITextView compared to the UILabel. Is there a setting in the UITextView I can change to make it display just like a UILabel?

Thanks in advance

UPDATE:

Here are the lines I used to make this work:

[[lblResults textContainer] setLineFragmentPadding:0.0];
[lblResults setTextContainerInset:UIEdgeInsetsZero];
[lblResults sizeToFit];

Thanks again for everyone's quick response and help.

Calvin
  • 15
  • 4

3 Answers3

3

A UITextView has padding, but a UILabel does not.

You can remove the padding like this on iOS 7+:

textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;

See this question for solutions pre-iOS 7.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks for the quick response Aaron. I tried the solution you posted. I am now able to view up to Line 132, but I am still missing 6 Lines unfortunately =( – Calvin Apr 05 '15 at 17:57
1

UIlabel is a view UITextView is a view too, the method boundingRectWithSize is not the same thing as a view frame is just the bounding rect to contain that text. As suggested by Aaron UITextView contains padding while UILabel not. To know the exact frame call -sizeToFit on the text container after setting its string and later ask for its frame.

Andrea
  • 26,120
  • 10
  • 85
  • 131
  • Andrea, thank you very much. A very simple answer. I made my textview "sizeToFit", used that as the frame for my scroll view, and all is good. +1 – Calvin Apr 05 '15 at 18:09
0

You could try setting lblResults.textContainer.maximumNumberOfLines to be a large number. (iOS 7)

Austin
  • 51
  • 5
  • Thank you for the quick response Austin. I tried your solution, but unfortunately I still can only scroll to Line 131. The bottom still gets cut off. – Calvin Apr 05 '15 at 17:56