Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the content in UITextView in ios

I want to load long text in TextViews of different Views. The text should be divided to pages when it reaches end of the textviews. And the next textview must start with the continuation of the text.

I have gone through a lot of answers. But all specifies about limiting text content when entering data. I want to display paged data in TextView. And there wont be any entering or editing in it. So the delegate methods regarding that won't work.

I tried to load text of fixed length. but it is not correct when the no.of paragraphs varies.

So what i am trying to find is , get notified when the the text reaches end of textview's capacity or getting the capacity of textview as per no lines/ no.of characters. Is there any way to do this ???

Update

As per the comments i have got, i searched a lot and reached at NSTextStorage, NSTextLayoutManager and NSTextContainer

I have found this link which will help to easily implement pagination

http://sketchytech.blogspot.co.uk/2013/11/paging-and-paginating-easy-way-with.html

I this example they have created 4 objects using loop. It can be said as they have splitted the string to 4 parts and displayed in 4 uitextviews of a scrollview.

I am trying to set a condition to split to textcontainers according to the length of string. I trying to create a condition by considering the total length of main string & the text displayed in textviews.

enter image description here

But each textview' text length is same as the total length of the main string. So how i could get the length text displayed in each textcontainer of textview ????

like image 251
Karan Alangat Avatar asked Feb 08 '14 10:02

Karan Alangat


2 Answers

You can create a CTFramesetter and call CTFramesetterSuggestFrameSizeWithConstraints. You will then find the range of text that fits within a given CGSize. Then manipulate the Text in accordance to the range and you are done.

Reference - CoreText.

like image 106
Ravi_Parmar Avatar answered Oct 11 '22 06:10

Ravi_Parmar


I had to do measurements of text in a UITextView a while ago for overlaying views on top of some words in a UITextView on iOS 5. With the UITextView changes for iOS 7 I no longer have to do this. The way I did measurement may be what you need to break up your text. I don't have a good working code to give so I will have to describe the algorithm as best I can remember it.

This algorithm does not work when there are variable line heights.

  1. I found by trial error that the text width I needed to match against was the width of the text view minus 16.
  2. Do a binary search on substrings of the text to find the maximum substring that fits in the UITexView where the substring breaks at word boundaries. You can use NSLinguistic tagger to get word boundaries.
  3. On each of the substring call [substr sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:NSLineBreakByWordWrapping] where the size constraints in the width from step 1 and a very large height.
  4. You will need to continue the search until you can identify the word boundary where the size with constraints for word1 gives you a height that fits in your view and the immediately next word gives you a height that does not fit in your view. You will know that this is where the UITextView will do word wrapping that would be too big to fit on one of your pages.
  5. Break your text on the word boundary from step 4 and do the same for the next page.
like image 43
Stephen Johnson Avatar answered Oct 11 '22 07:10

Stephen Johnson