0
votes

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.

3

3 Answers

2
votes

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.

1
votes

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.

0
votes

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