0

I have given a large text to display in different text views. So i have set the whole text content in an NSTextContainer.Then added a NSLayoutManager. Created NSTextContainers for different UITextViews. I got continuous text in different TextViews . But i cant limit the no of textviews using a condition.

My problem is i want to create textviews accroding to the content. And the content may vary.I tried to create a condition bw whole text content & textview's content. But even though each contains different text, all textview.text length returns the same value as the length of the entire text content !!!!

Here is the code>>

-(void)paginate:(NSString*)chapterData WithSize:(CGRect)frame
{
    int stringLength,totalTextViewsTextLength;
    totalTextViewsTextLength=0;


    NSString* htmlString = [NSString stringWithContentsOfFile:chapterData encoding:NSUTF8StringEncoding error:nil];

    NSAttributedString *temp=[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];


    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:temp];
    NSLayoutManager *textLayout = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:textLayout];

    stringLength=[temp length];


    while (totalTextViewsTextLength<stringLength)               // checks whether all the content has been added to textview or not
    {

        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:frame.size];
        [textLayout addTextContainer:textContainer];

//        textContainer.lin
        UITextView *textView=[[UITextView alloc]initWithFrame:frame textContainer:textContainer];
        textView.tag=textviewCount;



        // Store textviews in array
        [pagedData addObject:textView];


        totalTextViewsTextLength+=[textView.text length];
        textviewCount++;

    }

}

What i need is to get the length of text displayed in each textview(that contains different textcontainers ) ??????

Karan Alangat
  • 2,154
  • 4
  • 25
  • 56

4 Answers4

1

I get you point what exactly, you want. You have fix height of the text view, in this textView you want to set text, right ? If its right, then use following approach :

Suppose your textView height is 450

1, Calculate height of the your text, that you want to display in textView

here ft is your font size i.e UIFont * ft = [UIFont systemFontOfSize:12.0];

 CGSize expectedSize = [textview.text sizeWithAttributes:@{NSFontAttributeName:ft}];

    float height= 0; 

    height = expectedLabelSize.height 

2, check height of the textView with your text that you want to display in textView

if(height > 'your textview height') 
 {
     // add you code here 
 } 
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
0

to get count of visible characters first use code in this answer ios - how to find what is the visible range of text in UITextView? then just get length ofNSRange

Community
  • 1
  • 1
sage444
  • 5,661
  • 4
  • 33
  • 60
0

To get length of text in textview use:

 int textLength = [someTextView.text length]

To get length excluding white spaces use:

  int textLength= [someTextView.text  stringByTrimmingCharactersInSet:
                         [NSCharacterSet whitespaceAndNewlineCharacterSet]]length]; 

UPDATE To get length after every character entered u must have to write your code in following method

This will called for every character you type.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{   
  NSUInteger textLength = [textField.text length];
  NSLog(@"Textfield Length :%ld",textLength);
  return YES;
}
Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
iAhmed
  • 6,556
  • 2
  • 25
  • 31
-2

use following way, before set text to the textview, check length of the string :

NSString *textEntered = [[[textview.text copy] autorelease] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

if([textEntered length] > 450) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:kMoreThan450CharactersForQuestion delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
                [alertView release];
            }
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
  • Divya , you have just removed all white spaces and counted the text list. And limited the count to 450. How it will be correct when a text contains many paragraphs or no paragraphs ??? – Karan Alangat Feb 14 '14 at 12:02